閱讀549 返回首頁    go 阿裏雲 go 技術社區[雲棲]


C#文件、文件夾操作

using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Text; using System.IO; //文件操作所屬的命名空間。 namespace FileOperate { public class FileOperate//對文件的操作類 { //刪除文件方法。 public bool DeletFile(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); //利用FileInfo的.Name方法獲取文件名。 return F.Name; } 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(Filefullpath, Filefullpath + "");//第一個參數為原文件名,後一個參數為替換後的文件名。 } } 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 bool OpenFile(string Filefullpath) { if (File.Exists(Filefullpath)) { 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; //注意:這裏獲取文件的大小要用到long型數據,因為F.Length為long型。 if (FL > 1024 * 1024 * 1024) { // KB MB GB 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; } } //將文件轉換成二進製數。用來處理圖片等文件時用。 public byte[] FileToStreamByte(string Filefullpath) { byte[] FileData=null; if (File.Exists(Filefullpath) == true) { FileStream FS = new FileStream(Filefullpath, FileMode.Open); FileData = new byte[FS.Length]; FS.Read(FileData, 0, FileData.Length);//參數1:讀取的數組名,參數2:起始位置,參數3:讀取長度。 FS.Close(); return FileData; } else { return null; } } //將二進製數據轉換成文件。 public bool ByteStreamToFile(string CreateFilefullpath, byte[] SteamByte) { try { if (File.Exists(CreateFilefullpath) == true) { File.Delete(CreateFilefullpath); //如果該路徑存在文件,刪除該文件,該地址留給轉換後的文件。 } FileStream FS; FS=File.Create(CreateFilefullpath); FS.Write(SteamByte, 0, SteamByte.Length); FS.Close(); return true; } catch { return false; } } //為了實現不同係統間的交互,要利用XML傳遞數據,傳遞XML文件必須對它進行加密, //進行加密即是:序列化XML文件 public bool SerializeXmlFile(string Filefullpath) { try { System.Data.DataSet DS = new System.Data.DataSet(); DS.ReadXml(Filefullpath); //將要序列化的數據讀入DataSet數據集。 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);//參數1:圖形要序列化成的流,參數2:要序列化的數據。 FS.Close(); DeletFile(Filefullpath); File.Move(Filefullpath + ".tmp", Filefullpath); return true; } catch { return false; } } //反序列化XML文件。 public bool DeserializeFile(string Filefullpath) { try { System.Data.DataSet DS = new System.Data.DataSet(); DS.ReadXml(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(); DeletFile(Filefullpath); File.Move(Filefullpath + ".tmp", Filefullpath); return true; } catch { return false; } } } public class DirOperate //對文件夾的操作類。 { public enum OperateOption { ExistDelet, //存在,刪除再創建。 ExistReturn//存在,直接返回。 } //創建文件夾。 public bool CreatDir(string DirFullPath,OperateOption DirOperateOption) { try { if (Directory.Exists(DirFullPath) == false) { Directory.CreateDirectory(DirFullPath); //如果文件夾不存在,直接創建文件夾。 } else if (DirOperateOption == OperateOption.ExistDelet) { Directory.Delete(DirFullPath, true); } return true; } catch { return false; } } //刪除文件夾。 public bool DeletDir(string DirFullPath) { if (Directory.Exists(DirFullPath) == true) { Directory.Delete(DirFullPath); return true; } else { return false; } } //得到文件夾當前目錄下的所有文件。 public string[] GetDir(string DirFullPath) { string[] FileList=null; if (Directory.Exists(DirFullPath) == true) { FileList=Directory.GetFiles(DirFullPath,"*.*",SearchOption.TopDirectoryOnly);//參數1:文件夾路徑,參數2:要搜索的文件格式,參數3:指定搜索範圍 (當前目錄還是包含所有子目錄); } return FileList; } //得到文件夾目錄下的所有文件。 public string[] GetDir(string DirFullPath, SearchOption SO) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, "*.*", SO); } return FileList; } //搜索當前文件目錄下的指定文件格式的文件。 public string[] GetDir(string DirFullPath, string SeacherPattern) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, SeacherPattern); } return FileList; } //搜索文件目錄下的所有指定文件格式的文件。 public string[] GetDir(string DirFullPath, string SeacherPattern,SearchOption SO) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, SeacherPattern,SO); } return FileList; } }

最後更新:2017-04-02 04:00:23

  上一篇:go 2.Orcal中的SQL語句
  下一篇:go 深入了解trim方案