欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#对文件、文件夹、Xml等操作

程序员文章站 2022-06-12 08:27:31
...
 /// <summary>
/// 对文件操作类
/// </summary>
public class FileOperat
{
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="fileFullPath">要删除的文件路径</param>
/// <returns>true/false</returns>
public bool DeleteFile(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
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>文件名</returns>
public string GetFileName(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
FileInfo file = new FileInfo(fileFullPath);
return file.Name;
}
else
{
return null;
}
}

/// <summary>
/// 获取文件名
/// </summary>
/// <param name="fileFullPath">文件全路径</param>
/// <param name="includingFileExtension">是否包含扩展名</param>
/// <returns>string</returns>
public string GetFileName(string fileFullPath, bool includingFileExtension)
{
if (File.Exists(fileFullPath))
{
FileInfo fileInfo = new FileInfo(fileFullPath);
if (includingFileExtension == true)
{
return fileInfo.Name;
}
else
{
return fileInfo.Name.Replace(fileInfo.Extension, "");
}
}
else
{
return null;
}
}


/// <summary>
/// 获取文件扩展名
/// </summary>
/// <param name="fileFullPath">文件全路径</param>
/// <returns>string</returns>
public string GetFileExtension(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
FileInfo fileInfo = new FileInfo(fileFullPath);
return fileInfo.Extension;
}
else
{
return null;
}
}

/// <summary>
/// 根据传来的文件全路径,获取新的文件名称全路径(不存在),一般用如临时文件
/// </summary>
/// <param name="fileFullPath">文件名称全路径</param>
/// <returns>新的文件名称全路径</returns>
public string GetNewFileFullName(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
string TempFile = "";
FileInfo fileInfo=new FileInfo(fileFullPath);
for (int i = 0; i < 1000; i++)
{
TempFile = fileInfo.Name.Replace(fileInfo.Extension, "");
fileFullPath = TempFile + i.ToString() + fileInfo.Extension;
if (File.Exists(fileFullPath))
{
break; //如果文件存在,中断循环
}
}
}
return fileFullPath;
}

/// <summary>
/// 打开文件
/// </summary>
/// <param name="fileFullPath"></param>
/// <returns></returns>
public bool OpenFile(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
System.Diagnostics.Process.Start(fileFullPath);
return true;
}
else
{
return false;
}
}


/// <summary>
/// 得到文件的大小
/// </summary>
/// <param name="fileFullPath">文件全路径</param>
/// <returns>string</returns>
public string GetFileLength(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
FileInfo fileInfo = new FileInfo(fileFullPath);
long f = fileInfo.Length;
//KB MB GB TB
if (f > 1024 * 1024 * 1024)//GB
{
return Convert.ToString(Math.Round((f + 0.00) / (1024 * 1024 * 1024), 2)) + "GB";
}
if (f > 1024 * 1024) //MB
{
return Convert.ToString(Math.Round((f + 0.00) / (1024 * 1024), 2)) + "MB";
}
else
{
return Convert.ToString(Math.Round((f + 0.00) / 1024,2)) + "KB";
}
}
else
{
return null;
}
}

/// <summary>
/// 文件转换成二进制流
/// </summary>
/// <param name="fileFullPath">要转换的文件全路径</param>
/// <returns> Byte[] </returns>
/// <remarks></remarks>
public Byte[] FileToStreamByte(string fileFullPath)
{
Byte[] fileData = null;
if (File.Exists(fileFullPath))
{
FileStream fs = new FileStream(fileFullPath, System.IO.FileMode.Open);
fileData = new Byte[fs.Length];
fs.Read(fileData, 0, fileData.Length);
fs.Close();
return fileData;
}
else
{
return null;
}
}


/// <summary>
/// 从二进制转换成文件
/// </summary>
/// <param name="createFileFullPath">要转换的文件全路径</param>
/// <param name="streamByte">二进制流</param>
/// <returns>True/False</returns>
/// <remarks>True/False</remarks>
public bool ByteSteramToFile(string createFileFullPath, Byte[] streamByte)
{
try
{
FileStream fs = null;
if (File.Exists(createFileFullPath)) //如果文件存在,先删除
{
DeleteFile(createFileFullPath);
}
fs = File.Create(createFileFullPath);
fs.Write(streamByte,0, streamByte.Length);
fs.Close();
return true;
}
catch
{
return false;
}
}


/// <summary>
/// 序列化Xml文件
/// </summary>
/// <param name="fileFullPath">要序列化的xml文件路径</param>
/// <returns>True/False</returns>
public bool SerializeXmlFile(string fileFullPath)
{
System.Data.DataSet ds = new System.Data.DataSet();
if (File.Exists(fileFullPath))
{
try
{
ds.ReadXml(fileFullPath);
FileStream fs = new FileStream(fileFullPath + ".tmp", FileMode.OpenOrCreate);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
BF.Serialize(fs, ds);//序例化ds,并将结果赋给fs
fs.Close();
DeleteFile(fileFullPath);//删除传入的xml文件
File.Move(fileFullPath + ".tmp", fileFullPath);//将序列化后的文件改为原来的Xml文件
return true;
}
catch
{
return false;
}
}
else
{
return false;
}

}

/// <summary>
/// 反序列化Xml
/// </summary>
/// <param name="fileFullPath">反序列化的Xml文件路径</param>
/// <returns>True/False</returns>
/// <remarks></remarks>
public bool DescSerializeXmlFile(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
FileStream fs = new FileStream(fileFullPath, FileMode.Open);//打开xml文件,将内容读入到流中
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
object descSeriResult = BF.Deserialize(fs);//反序列化
((System.Data.DataSet)descSeriResult).WriteXml(fs + ".tmp");
fs.Close();//关闭流
DeleteFile(fileFullPath);//删除传入的xml文件
File.Move(fileFullPath + ".tmp", fileFullPath);//将反序列化后的文件改名传入的xml文件
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
}


/// <summary>
/// 对文件目录操作的公共组件
/// </summary>
public class DirOperate
{
public enum OperateOption
{
/// <summary>
/// 文件夹存在时删除
/// </summary>
ExistDelete,
/// <summary>
/// 文件夹存在时返回
/// </summary>
ExistReturn
}

/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <param name="dirOperateOption">文件夹操作选项</param>
/// <returns>True/False</returns>
public bool CreateDirOperate(string dirFullPath, OperateOption dirOperateOption)
{
try
{
if (!Directory.Exists(dirFullPath))
{
Directory.CreateDirectory(dirFullPath);
}
else if (dirOperateOption == OperateOption.ExistDelete)
{
Directory.Delete(dirFullPath,true);
Directory.CreateDirectory(dirFullPath);
}
return true;
}
catch
{
return false;
}
}

/// <summary>
/// 删除文件夹操作
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <returns>True/False</returns>
public bool DeleteDirOperate(string dirFullPath)
{
if (Directory.Exists(dirFullPath))
{
Directory.Delete(dirFullPath, true);
return true;
}
else
{
return false;
}
}


/// <summary>
/// 得到文件夹下的所有文件,不包含子文件夹
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <returns>string[]</returns>
/// <remarks></remarks>
public string[] GetDirFiles(string dirFullPath)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, "*.*", SearchOption.TopDirectoryOnly);

}
else
{
return null;
}
}


/// <summary>
/// 得到文件夹下的所有文件,按指定的搜索模式
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <param name="so">搜索类型</param>
/// <returns>string[]</returns>
/// <remarks></remarks>
public string[] GetDirFiles(string dirFullPath,SearchOption so)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, "*.*",so);

}
else
{
return null;
}
}


/// <summary>
/// 得到文件夹下所有与文件类型匹配的文件
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <param name="SearchPattern">文件类型</param>
/// <returns></returns>
public string[] GetDirFiles(string dirFullPath,string SearchPattern)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, SearchPattern);

}
else
{
return null;
}
}


/// <summary>
/// 得到文件夹下所有文件
/// </summary>
/// <param name="dirFullPath">文件夹路径</param>
/// <param name="SearchPattern">文件类型</param>
/// <param name="so">搜索类型</param>
/// <returns>string[]</returns>
/// <remarks></remarks>
public string[] GetDirFiles(string dirFullPath, string SearchPattern,SearchOption so)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, SearchPattern,so);

}
else
{
return null;
}
}

}