C#中文件与文件夹相关的操作
-
-
-
- public class FileOperate
- {
-
-
-
-
-
- public bool DeleteFile(string FileFullPath)
- {
- if (File.Exists(FileFullPath) == true)
- {
- File.SetAttributes(FileFullPath, FileAttributes.Normal);
- File.Delete(FileFullPath);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
- public string GetFileName(string FileFullPath)
- {
- if (File.Exists(FileFullPath) == true)
- {
- FileInfo F = new FileInfo(FileFullPath);
- return F.Name;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
- public string GetFileExtension(string FileFullPath)
- {
- if (File.Exists(FileFullPath) == true)
- {
- FileInfo F = new FileInfo(FileFullPath);
- return F.Extension;
-
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
- public bool OpenFile(string FileFullPath)
- {
- if (File.Exists(FileFullPath) == true)
- {
- System.Diagnostics.Process.Start(FileFullPath);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
- 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 Math.Round((FL + 0.00) / (1024 * 1024 * 1024), 2).ToString() + " GB";
- }
- else if (FL > (1024 * 1024))
- {
- return Math.Round((FL + 0.00) / (1024 * 1024), 2).ToString() + " MB";
- }
- else if (FL > 1024)
- {
- return Math.Round((FL + 0.00) / 1024, 2).ToString() + " KB";
- }
- else
- {
- return FL.ToString();
- }
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
- public byte FileToStreamByte(string FileFullPath)
- {
- if (File.Exists(FileFullPath) == true)
- {
- FileStream FS = new FileStream(FileFullPath, FileMode.Open);
- byte fileData = new byte[FS.Length];
- FS.Read(fileData, 0, fileData.Length);
- FS.Close();
- return fileData;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
- public bool ByteStreamToFile(string FileFullPath, byte StreamByte)
- {
- try
- {
- if (File.Exists(FileFullPath) == true)
- {
- File.Delete(FileFullPath);
- }
- FileStream FS = new FileStream(FileFullPath, FileMode.OpenOrCreate);
- FS.Write(StreamByte, 0, StreamByte.Length);
- FS.Close();
- return true;
- }
- catch
- {
- return false;
- }
- }
-
-
-
-
-
- public bool SerializeXml(string FileFullPath)
- {
- try
- {
- System.Data.DataSet DS = new System.Data.DataSet();
- DS.ReadXml(FileFullPath);
- 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();
- DeleteFile(FileFullPath);
- File.Move(FileFullPath + ".tmp", FileFullPath);
- return true;
- }
- catch
- {
- return false;
- }
- }
-
-
-
-
-
- public bool DeSerializeXml(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();
- DeleteFile(FileFullPath);
- File.Move(FileFullPath + ".tmp", FileFullPath);
- return true;
- }
- }
-
-
-
- public class DirOperate
- {
- public enum OperateOption
- {
-
-
-
- ExistDelete,
-
-
-
- ExistReturn
- }
-
-
-
-
-
-
- public bool CreateDir(string DirFullPath, OperateOption Option)
- {
- try
- {
- if (Directory.Exists(DirFullPath) == false)
- {
- Directory.CreateDirectory(DirFullPath);
- }
- else if (Option == OperateOption.ExistDelete)
- {
- Directory.Delete(DirFullPath);
- Directory.CreateDirectory(DirFullPath);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- public bool DeleteDir(string DirFullPath)
- {
- if (Directory.Exists(DirFullPath) == true)
- {
- Directory.Delete(DirFullPath, true);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
- public string GetDirFiles(string DirFullPath)
- {
- if (Directory.Exists(DirFullPath) == true)
- {
- string s = Directory.GetFiles(DirFullPath, "*.*", SearchOption.TopDirectoryOnly);
- return s;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
- public string GetDirFiles(string DirFullPath, SearchOption SO)
- {
- if (Directory.Exists(DirFullPath) == true)
- {
- string s = Directory.GetFiles(DirFullPath, "*.*", SearchOption.AllDirectories);
- return s;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
- public string GetDirFiles(string DirFullPath, string SearchPattern)
- {
- if (Directory.Exists(DirFullPath) == true)
- {
- string s = Directory.GetFiles(DirFullPath, SearchPattern);
- return s;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
-
- public string GetDirFiles(string DirFullPath, string SearchPattern, SearchOption SO)
- {
- if (Directory.Exists(DirFullPath) == true)
- {
- string s = Directory.GetFiles(DirFullPath, SearchPattern, SO);
- return s;
- }
- else
- {
- return null;
- }
- }
- }