文件IO 操作类库,通用成熟的代码,现分享出来,为程序员提供最优秀的源代码。
时间:2011-02-28 来源:ffffff
namespace HaiRiHan.Class.IO
{
/// <summary>
/// 文件有关的操作类
/// </summary>
public class FileOperate
{
/// <summary>
/// 删除文件
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>bool 是否删除成功</returns>
public bool DeleteFile(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
if (File.GetAttributes(FileFullPath) == FileAttributes.Normal)
{
File.Delete(FileFullPath);
}
else
{
File.SetAttributes(FileFullPath, FileAttributes.Normal);
File.Delete(FileFullPath);
}
return true;
}
else//文件不存在
{
return false;
}
}
/// <summary>
/// 根据传来的文件全路径,获取文件名称部分默认包括扩展名
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>string 文件名称</returns>
public string GetFileName(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
FileInfo F = new FileInfo(FileFullPath);
return F.Name;
}
else//文件不存在
{
return null;
}
}
/// <summary>
/// 根据传来的文件全路径,获取文件名称部分
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <param name="IncludeExtension">是否包括文件扩展名</param>
/// <returns>string 文件名称</returns>
public string GetFileName(string FileFullPath, bool IncludeExtension)
{
if (File.Exists(FileFullPath) == true)
{
FileInfo F = new FileInfo(FileFullPath);
if (IncludeExtension == true)
{
return F.Name;
}
else
{
return F.Name.Replace(F.Extension, "");
}
}
else//文件不存在
{
return null;
}
}
/// <summary>
/// 根据传来的文件全路径,获取新的文件名称全路径,一般用作临时保存用
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>string 新的文件全路径名称</returns>
public string GetNewFileFullName(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
string tempFileName = "";
FileInfo F = new FileInfo(FileFullPath);
tempFileName = FileFullPath.Replace(F.Extension, "");
for (int i = 0; i < 1000; i++)
{
FileFullPath = tempFileName + i.ToString() + F.Extension;
if (File.Exists(FileFullPath) == false)
{
break;
}
}
}
return FileFullPath;
}
/// <summary>
/// 根据传来的文件全路径,获取文件扩展名不包括“.”,如“doc”
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>string 文件扩展名</returns>
public string GetFileExtension(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
FileInfo F = new FileInfo(FileFullPath);
return F.Extension;
}
else//文件不存在
{
return null;
}
}
/// <summary>
/// 根据传来的文件全路径,外部打开文件,默认用系统注册类型关联软件打开
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>bool 文件名称</returns>
public bool OpenFile(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
System.Diagnostics.Process.Start(FileFullPath);
return true;
}
else//文件不存在
{
return false;
}
}
/// <summary>
/// 根据传来的文件全路径,得到文件大小,规范文件大小称呼,如1GB以上,单位用GB,1MB以上,单位用MB,1MB以下,单位用KB
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>bool 文件大小</returns>
public string GetFileSize(string FileFullPath)
{
if (File.Exists(FileFullPath) == true)
{
FileInfo F = new FileInfo(FileFullPath);
long FL = F.Length;
if (FL > 1024 * 1024 * 1024)
{
return System.Convert.ToString(Math.Round((FL + 0.00) / (1024 * 1024 * 1024), 2)) + " GB";
}
else if (FL > 1024 * 1024)
{
return System.Convert.ToString(Math.Round((FL + 0.00) / (1024 * 1024), 2)) + " MB";
}
else
{
return System.Convert.ToString(Math.Round((FL + 0.00) / 1024, 2)) + " KB";
}
}
else//文件不存在
{
return null;
}
}
/// <summary>
/// 文件转换成二进制,返回二进制数组Byte[]
/// </summary>
/// <param name="FileFullPath">文件全路径</param>
/// <returns>byte[] 包含文件流的二进制数组</returns>
public byte[] FileToStreamByte(string FileFullPath)
{
byte[] fileData = null;
if (File.Exists(FileFullPath) == true)
{
FileStream Fs = new FileStream(FileFullPath, System.IO.FileMode.Open);
fileData = new byte[Fs.Length];
Fs.Read(fileData, 0, fileData.Length);
Fs.Close();
}
else//文件不存在
{
}
return fileData;
}
/// <summary>
/// 二进制数组Byte[]生成文件
/// </summary>
/// <param name="CreateFileFullPath">要生成的文件全路径</param>
/// <param name="StreamByte">要生成文件的二进制 Byte 数组</param>
/// <returns>bool 是否生成成功</returns>
public bool ByteStreamToFile(string CreateFileFullPath, byte[] StreamByte)
{
if (File.Exists(CreateFileFullPath) == false)
{
FileStream FS;
FS = File.Create(CreateFileFullPath);
FS.Write(StreamByte, 0, StreamByte.Length);
FS.Close();
return true;
}
else//文件已存在
{
return false;
}
}
/// <summary>
/// 二进制数组Byte[]生成文件,并验证文件是否存在,在存则先删除
/// </summary>
/// <param name="CreateFileFullPath">要生成的文件全路径</param>
/// <param name="StreamByte">要生成文件的二进制 Byte 数组</param>
/// <param name="FileExistsDelete">同路径文件存在是否先删除</param>
/// <returns>bool 是否生成成功</returns>
public bool ByteStreamToFile(string CreateFileFullPath, byte[] StreamByte, bool FileExistsDelete)
{
if (File.Exists(CreateFileFullPath) == true)
{
if (FileExistsDelete == true && DeleteFile(CreateFileFullPath) == false)
{
return false;
}
}
FileStream FS;
FS = File.Create(CreateFileFullPath);
FS.Write(StreamByte, 0, StreamByte.Length);
FS.Close();
return true;
}
/// <summary>
/// 将Xml文件反序列化。
/// </summary>
/// <param name="FileFullPath">要反序列化的文件全路径</param>
/// <returns>bool 是否反序列化成功</returns>
public bool DeserializeXmlFile(string FileFullPath)
{
FileStream FS = new FileStream(FileFullPath, FileMode.Open);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter FT = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
((System.Data.DataSet)FT.Deserialize(FS)).WriteXml(FileFullPath + ".tmp");
FS.Close();
File.Delete(FileFullPath);
File.Move(FileFullPath + ".tmp", FileFullPath);
return true;
}
/// <summary>
/// 将Xml文件序列化。
/// </summary>
/// <param name="FileFullPath">要序列化的文件全路径</param>
/// <returns>bool 是否序列化成功</returns>
public bool SerializeXmlFile(string FileFullPath)
{
System.Data.DataSet DS = new System.Data.DataSet();
DS.ReadXml(FileFullPath);
HaiRiHan.Class.IO.FileOperate F = new FileOperate();
if (File.Exists(FileFullPath + ".tmp") == true)
{
F.DeleteFile(FileFullPath + ".tmp");
}
FileStream FS = new FileStream(FileFullPath + ".tmp", FileMode.OpenOrCreate);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter FT = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
FT.Serialize(FS, DS);
FS.Close();
F.DeleteFile(FileFullPath);
File.Move(FileFullPath + ".tmp", FileFullPath);
return true;
}
}
/// <summary>
/// 文件夹有关的操作类
/// </summary>
public class DirOperate
{
/// <summary>
/// 创建文件夹时的操作选项
/// </summary>
public enum OperateOption
{
/// <summary>
/// 当被创建的文件夹存在时,先删除该文件夹
/// </summary>
ExistDelete,
/// <summary>
/// 当文件夹存在时,直接返回,不进行其它操作
/// </summary>
ExistReturn
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="DirFullPath">要创建文件夹的全路径</param>
/// <param name="DirOperateOption">创建文件夹时的操作选项</param>
/// <returns>bool 是否创建成功</returns>
public bool CreateDir(string DirFullPath, HaiRiHan.Class.IO.DirOperate.OperateOption DirOperateOption)
{
if (Directory.Exists(DirFullPath) == false)
{
Directory.CreateDirectory(DirFullPath);
}
else if (DirOperateOption == HaiRiHan.Class.IO.DirOperate.OperateOption.ExistDelete)
{
Directory.Delete(DirFullPath, true);
Directory.CreateDirectory(DirFullPath);
}
return true;
}
/// <summary>
/// 复制文件夹
/// </summary>
/// <param name="strFromDirectory">要复制的文件夹</param>
/// <param name="strToDirectory">复制到的文件夹</param>
/// <returns></returns>
public bool CopyDir(string strFromDirectory, string strToDirectory)
{
Directory.CreateDirectory(strToDirectory);
if (!Directory.Exists(strFromDirectory)) return false;
string[] directories = Directory.GetDirectories(strFromDirectory);
if (directories.Length > 0)
{
foreach (string d in directories)
{
CopyDir(d, strToDirectory + d.Substring(d.LastIndexOf("\\")));
}
}
string[] files = Directory.GetFiles(strFromDirectory);
if (files.Length > 0)
{
foreach (string s in files)
{
File.Copy(s, strToDirectory + s.Substring(s.LastIndexOf("\\")));
}
}
return true;
}
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="DirFullPath">要删除文件夹的全路径</param>
/// <returns>bool 是否删除成功</returns>
public bool DeleteDir(string DirFullPath)
{
if (Directory.Exists(DirFullPath) == true)
{
Directory.Delete(DirFullPath, true);
}
else//文件夹不存在
{
return false;
}
return true;
}
/// <summary>
/// 得到当前文件夹中所有文件列表string[]
/// </summary>
/// <param name="DirFullPath">要遍历的文件夹全路径</param>
/// <returns>string[] 文件列表</returns>
public string[] GetDirFiles(string DirFullPath)
{
string[] FileList = null;
if (Directory.Exists(DirFullPath) == true)
{
FileList = Directory.GetFiles(DirFullPath, "*.*", System.IO.SearchOption.TopDirectoryOnly);
}
else//文件夹不存在
{
return FileList;
}
return FileList;
}
/// <summary>
/// 得到当前文件夹及下级文件夹中所有文件列表string[]
/// </summary>
/// <param name="DirFullPath">要遍历的文件夹全路径</param>
/// <param name="SO">查找文件的选项,是否包含子级文件夹</param>
/// <returns>string[] 文件列表</returns>
public string[] GetDirFiles(string DirFullPath, System.IO.SearchOption SO)
{
string[] FileList = null;
if (Directory.Exists(DirFullPath) == true)
{
FileList = Directory.GetFiles(DirFullPath, "*.*", SO);
}
else//文件夹不存在
{
return FileList;
}
return FileList;
}
/// <summary>
/// 得到当前文件夹中指定文件类型[扩展名]文件列表string[]
/// </summary>
/// <param name="DirFullPath">要遍历的文件夹全路径</param>
/// <param name="SearchPattern">查找文件的扩展名如“*.*代表所有文件;*.doc代表所有doc文件”</param>
/// <returns>string[] 文件列表</returns>
public string[] GetDirFiles(string DirFullPath, string SearchPattern)
{
string[] FileList = null;
if (Directory.Exists(DirFullPath) == true)
{
FileList = Directory.GetFiles(DirFullPath, SearchPattern);
}
else//文件夹不存在
{
return FileList;
}
return FileList;
}
/// <summary>
/// 得到当前文件夹及下级文件夹中指定文件类型[扩展名]文件列表string[]
/// </summary>
/// <param name="DirFullPath">要遍历的文件夹全路径</param>
/// <param name="SearchPattern">查找文件的扩展名如“*.*代表所有文件;*.doc代表所有doc文件”</param>
/// <param name="SO">查找文件的选项,是否包含子级文件夹</param>
/// <returns>string[] 文件列表</returns>
public string[] GetDirFiles(string DirFullPath, string SearchPattern, System.IO.SearchOption SO)
{
string[] FileList = null;
if (Directory.Exists(DirFullPath) == true)
{
FileList = Directory.GetFiles(DirFullPath, SearchPattern, SO);
}
else//文件夹不存在
{
return FileList;
}
return FileList;
}
}
}
通用权限快速开发框架营销