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

 /// 
/// 对文件操作类
///

public class FileOperat
{
///
/// 删除指定文件
///

/// 要删除的文件路径
/// true/false
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;
}
}

///
/// 获取文件名
///

/// 文件全路径
/// 文件名
public string GetFileName(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
FileInfo file = new FileInfo(fileFullPath);
return file.Name;
}
else
{
return null;
}
}

///
/// 获取文件名
///

/// 文件全路径
/// 是否包含扩展名
/// string
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;
}
}


///
/// 获取文件扩展名
///

/// 文件全路径
/// string
public string GetFileExtension(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
FileInfo fileInfo = new FileInfo(fileFullPath);
return fileInfo.Extension;
}
else
{
return null;
}
}

///
/// 根据传来的文件全路径,获取新的文件名称全路径(不存在),一般用如临时文件
///

/// 文件名称全路径
/// 新的文件名称全路径
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;
}

///
/// 打开文件
///

///
///
public bool OpenFile(string fileFullPath)
{
if (File.Exists(fileFullPath))
{
System.Diagnostics.Process.Start(fileFullPath);
return true;
}
else
{
return false;
}
}


///
/// 得到文件的大小
///

/// 文件全路径
/// string
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;
}
}

///
/// 文件转换成二进制流
///

/// 要转换的文件全路径
/// Byte[]
///
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;
}
}


///
/// 从二进制转换成文件
///

/// 要转换的文件全路径
/// 二进制流
/// True/False
/// True/False
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;
}
}


///
/// 序列化Xml文件
///

/// 要序列化的xml文件路径
/// True/False
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;
}

}

///
/// 反序列化Xml
///

/// 反序列化的Xml文件路径
/// True/False
///
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;
}
}
}


///
/// 对文件目录操作的公共组件
///

public class DirOperate
{
public enum OperateOption
{
///
/// 文件夹存在时删除
///

ExistDelete,
///
/// 文件夹存在时返回
///

ExistReturn
}

///
/// 创建文件夹
///

/// 文件夹路径
/// 文件夹操作选项
/// True/False
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;
}
}

///
/// 删除文件夹操作
///

/// 文件夹路径
/// True/False
public bool DeleteDirOperate(string dirFullPath)
{
if (Directory.Exists(dirFullPath))
{
Directory.Delete(dirFullPath, true);
return true;
}
else
{
return false;
}
}


///
/// 得到文件夹下的所有文件,不包含子文件夹
///

/// 文件夹路径
/// string[]
///
public string[] GetDirFiles(string dirFullPath)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, "*.*", SearchOption.TopDirectoryOnly);

}
else
{
return null;
}
}


///
/// 得到文件夹下的所有文件,按指定的搜索模式
///

/// 文件夹路径
/// 搜索类型
/// string[]
///
public string[] GetDirFiles(string dirFullPath,SearchOption so)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, "*.*",so);

}
else
{
return null;
}
}


///
/// 得到文件夹下所有与文件类型匹配的文件
///

/// 文件夹路径
/// 文件类型
///
public string[] GetDirFiles(string dirFullPath,string SearchPattern)
{
string[] fileList = null;
if (Directory.Exists(dirFullPath))
{
return fileList = Directory.GetFiles(dirFullPath, SearchPattern);

}
else
{
return null;
}
}


///
/// 得到文件夹下所有文件
///

/// 文件夹路径
/// 文件类型
/// 搜索类型
/// string[]
///
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;
}
}

}

你可能感兴趣的:(C#)