常用操作文件DLL类
/// <summary>
/// 关于文件的所有操作类
/// </summary>
public class FileOption
{
/// <summary>
/// 是否删除成功
/// </summary>
/// <param name="FileFullPath">删除文件全路径</param>
/// <returns>是否删除成功</returns>
public bool DeleteFile(string FileFullPath)
{
try
{
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;
}
catch
{
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="IncludeExtention"></param>
/// <returns></returns>
public string GetFileName(string FileFullPath, bool IncludeExtention)
{
if (File.Exists(FileFullPath))
{
FileInfo fileinfo=new FileInfo(FileFullPath);
if (IncludeExtention)
{
return fileinfo.Name;
}
else
{
return fileinfo.Name.Replace(fileinfo.Extension, "");
}
}
else
{
return null;
}
}
/// <summary>
/// 得到扩展名
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public String GetFileExtent(string FileFullPath)
{
if (File.Exists(FileFullPath))
{
FileInfo file = new FileInfo(FileFullPath);
return file.Extension;
}
else
{
return null;
}
}
/// <summary>
/// 是否打开文件
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public bool OpentFile(string FileFullPath)
{
if (File.Exists(FileFullPath))
{
System.Diagnostics.Process.Start(FileFullPath);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 得到文件大小
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public string GetFileSize(string FileFullPath)
{
if (File.Exists(FileFullPath))
{
FileInfo fileInfo = new FileInfo(FileFullPath);
long fileLength = fileInfo.Length;
if (fileLength > 1024 * 1024 * 1024)
{
return Math.Round((fileLength + 0.0) / (1024 * 1024*1024), 2).ToString()+"G";
}
else if (fileLength > 1024 * 1024)
{
return Math.Round((fileLength + 0.0) / (1024 * 1024), 2).ToString() + "m";
}
else
{
return Math.Round((fileLength + 0.0) / (1024), 2).ToString() + "kb";
}
}
else
{
return "0";
}
}
/// <summary>
/// 将文件转变为字节
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public byte[] FileToStreanByte(string FileFullPath)
{
byte[] fileData = null;
if (File.Exists(FileFullPath))
{
FileStream fs = new FileStream(FileFullPath, FileMode.OpenOrCreate);
fileData = new byte[fs.Length];
fs.Read(fileData, 0, fileData.Length);
fs.Close();
}
return fileData;
}
/// <summary>
///
/// </summary>
/// <param name="FileFullPath"></param>
/// <param name="Bytesfile"></param>
/// <returns></returns>
public bool ByteToFileStrean(string FileFullPath,byte[] Bytesfile)
{
try
{
if (File.Exists(FileFullPath))
{
File.Delete(FileFullPath);
}
FileStream fs = File.Create(FileFullPath);
fs.Write(Bytesfile, 0, Convert.ToInt32(fs.Length));
fs.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public bool SerializeXmlFile(string FileFullPath)
{
try
{
System.Data.DataSet DS = new System.Data.DataSet();
DS.WriteXml(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);
FS.Close();
DeleteFile(FileFullPath);
File.Move(FileFullPath + ".TMP", FileFullPath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 反序列XML
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
public bool DeserialzeXmlFile(string FileFullPath)
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
FileStream FS = new FileStream(FileFullPath, FileMode.OpenOrCreate);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
((System.Data.DataSet)BF.Deserialize(FS)).WriteXml(FileFullPath + ".TMP");
FS.Close();
DeleteFile(FileFullPath);
File.Move(FileFullPath + ".TMP", FileFullPath);
return true;
}
catch
{
return false;
}
}
}