文件相关操作

using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace BrainSoft.Common
{
 /// <summary>
 /// 描    述:文件相关操作
 /// 参数说明:
 /// 说    明:
 /// 创 建 人:thanktheworld

 /// 日    期:now

 /// 历史修改记录:
 /// </summary>
 public class FileUtil
 {
  #region 返回文件二进制信息
  /// <summary>
  /// 返回文件二进制信息
  /// </summary>
  public static byte[] GetFileBytesInfo(string filePath)
  {
   FileInfo myFile = new FileInfo(filePath);
   FileStream myFs = myFile.OpenRead();
   byte[] bytes = new byte[myFs.Length];
   myFs.Read(bytes, 0, Convert.ToInt32(myFs.Length));
   myFs.Close();
   myFs = null; myFile = null;
   return bytes;
  }
  #endregion

  #region 保存二进制信息成文件
  /// <summary>
  /// 保存二进制信息成文件
  /// </summary>
  public static bool SaveBytesToFile(byte[] _file, string filePath)
  {
   FileStream fs = new FileStream(filePath, FileMode.CreateNew);
   BinaryWriter bw = new BinaryWriter(fs);
   bw.Write(_file, 0, _file.Length);
   bw.Close();
   fs.Close();
   return true;
  }
  #endregion

  #region 根据路径创建文件夹
  /// <summary>
  /// 根据路径创建文件夹
  /// </summary>
  /// <param name="filePath">文件路径</param>
  public static bool CreateFolder(string filePath)
  {
   string[] folders=filePath.Split('\\');
   string folderPath=folders[0];
   for (int i=1;i<folders.Length-1;i++)
   {
    folderPath=folderPath+@""+folders[i];
    if (Directory.Exists (folderPath)==false)
    {
     Directory.CreateDirectory(folderPath);
    }
   }
   return true;
  }
  #endregion

  #region 获取临时文件
  /// <summary>
  /// 获取临时文件
  /// </summary>
  /// <returns></returns>
  public static string GetTempFileName()
  {
   return GetTempFileName("");
  }

  /// <summary>
  /// 获取临时文件
  /// </summary>
  /// <param name="Relative"></param>
  /// <returns></returns>
  public static string GetTempFileName(string Relative)
  {
   string path  = Global.TempPath;
   if (path.EndsWith(@"")==false)
   {
    path  = path  +@"\"+ Relative ;
   }
   else
   {
    path  = path  + Relative ;
   }
   if (!System.IO.Directory.Exists(path))
   {
    System.IO.Directory.CreateDirectory(path);
   }
   path += System.Guid.NewGuid().ToString();
   return path;
  }
  #endregion

  #region 服务器路径转换为本地路径
  /// <summary>
  /// 服务器路径转换为本地路径
  /// </summary>
  /// <param name="serverPath">服务器路径为 \表名\字锻名\主键.扩展名</param>
  /// <returns></returns>
  public static string ConvertServerToLocal(string serverPath)
  {
   if (serverPath.IndexOf(":")>-1)
   {
    return serverPath;
   }
   else
   {
    return Global.TempPath + serverPath;
   }
  }

  /// <summary>
  /// 服务器路径转换为本地路径
  /// </summary>
  /// <param name="serverPath">服务器路径为 \表名\字锻名\主键.扩展名</param>
  /// <returns></returns>
  public static string ConvertServerToLocalTemp(string serverPath)
  {
   string[] folders=serverPath.Split('\\');
   string folderPath=folders[0];
   for (int i=1;i<folders.Length-1;i++)
   {
    folderPath=folderPath+@""+folders[i];
   }
   return Global.TempPath + folderPath + @""+ System.Guid.NewGuid().ToString() + "." + GetExtName(serverPath);
  }
  #endregion

  #region 本地路径转换为服务器路径
  /// <summary>
  /// 文件路径转换
  /// </summary>
  /// <param name="PK">主键</param>
  /// <param name="serverPath">服务器路径</param>
  /// <param name="filePath">本地路径</param>
  /// <returns>\表名\字锻名\主键.扩展名</returns>
  public static string ConvertLocalToServer(string PK,string serverPath,string filePath)
  {
   return serverPath + PK + "." + GetExtName(filePath);
  }
  #endregion

  #region 获取文件扩展名
  /// <summary>
  /// 获取文件扩展名
  /// </summary>
  /// <param name="filePath">文件路径</param>
  /// <returns></returns>
  public static string GetExtName(string filePath)
  {
   string[] arr = filePath.Split('.');
   return arr[arr.Length - 1];
  
  #endregion
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值