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
}
}