場景
C#中File類的常用讀取與寫入檔案方法的使用:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
獲取檔案的擴展名
/// <summary> /// 獲取檔案的擴展名 /// </summary> /// <param name="filename">完整檔案名</param> /// <returns>回傳擴展名</returns> public static string GetPostfixStr(string filename) { int num = filename.LastIndexOf("."); int length = filename.Length; return filename.Substring(num, length - num); }
讀取檔案內容
/// <summary> /// 讀取檔案內容 /// </summary> /// <param name="path">要讀取的檔案路徑</param> /// <returns>回傳檔案內容</returns> public static string ReadFile(string path) { string result; if (!System.IO.File.Exists(path)) { result = "不存在相應的目錄"; } else { System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default); result = streamReader.ReadToEnd(); streamReader.Close(); streamReader.Dispose(); } return result; }
指定編碼格式讀取檔案內容
/// <summary> /// 讀取檔案內容 /// </summary> /// <param name="path">要讀取的檔案路徑</param> /// <param name="encoding">編碼格式</param> /// <returns>回傳檔案內容</returns> public static string ReadFile(string path, System.Text.Encoding encoding) { string result; if (!System.IO.File.Exists(path)) { result = "不存在相應的目錄"; } else { System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding); result = streamReader.ReadToEnd(); streamReader.Close(); streamReader.Dispose(); } return result; }
向指定檔案寫入內容
/// <summary> /// 向指定檔案寫入內容 /// </summary> /// <param name="path">要寫入內容的檔案完整路徑</param> /// <param name="content">要寫入的內容</param> public static void WriteFile(string path, string content) { try { object obj = new object(); if (!System.IO.File.Exists(path)) { System.IO.FileStream fileStream = System.IO.File.Create(path); fileStream.Close(); } lock (obj) { using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default)) { streamWriter.WriteLine(content); streamWriter.Close(); streamWriter.Dispose(); } } } catch (System.Exception ex) { ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("寫入檔案{0}例外:{1}", path, ex.Message), ex); } }
指定編碼格式向檔案寫入內容
/// <summary> /// 向指定檔案寫入內容 /// </summary> /// <param name="path">要寫入內容的檔案完整路徑</param> /// <param name="content">要寫入的內容</param> /// <param name="encoding">編碼格式</param> public static void WriteFile(string path, string content, System.Text.Encoding encoding) { try { object obj = new object(); if (!System.IO.File.Exists(path)) { System.IO.FileStream fileStream = System.IO.File.Create(path); fileStream.Close(); } lock (obj) { using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding)) { streamWriter.WriteLine(content); streamWriter.Close(); streamWriter.Dispose(); } } } catch (System.Exception ex) { ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("寫入檔案{0}例外:{1}", path, ex.Message), ex); } }
檔案復制
/// <summary> /// 檔案復制 /// </summary> /// <param name="orignFile">源檔案完整路徑</param> /// <param name="newFile">目標檔案完整路徑</param> public static void FileCoppy(string orignFile, string newFile) { System.IO.File.Copy(orignFile, newFile, true); }
檔案洗掉
/// <summary> /// 洗掉檔案 /// </summary> /// <param name="path">要洗掉的檔案的完整路徑</param> public static void FileDel(string path) { System.IO.File.Delete(path); }
檔案移動
/// <summary> /// 檔案移動(剪貼->粘貼) /// </summary> /// <param name="orignFile">源檔案的完整路徑</param> /// <param name="newFile">目標檔案完整路徑</param> public static void FileMove(string orignFile, string newFile) { System.IO.File.Move(orignFile, newFile); }
判斷一組檔案是否都存在
/// <summary> /// 判斷一組檔案是否都存在 /// </summary> /// <param name="filePathList">檔案路徑List</param> /// <returns>檔案是否全部存在</returns> public static bool IsFilesExist(List<string> filePathList) { bool isAllExist = true; foreach(string filePath in filePathList) { if(!File.Exists(filePath)) { isAllExist = false; } } return isAllExist; }
創建目錄
/// <summary> /// 創建目錄 /// </summary> /// <param name="orignFolder">當前目錄</param> /// <param name="newFloder">要創建的目錄名</param> public static void FolderCreate(string orignFolder, string newFloder) { System.IO.Directory.SetCurrentDirectory(orignFolder); System.IO.Directory.CreateDirectory(newFloder); }
洗掉目錄
/// <summary> /// 洗掉目錄 /// </summary> /// <param name="dir">要洗掉的目錄</param> public static void DeleteFolder(string dir) { if (System.IO.Directory.Exists(dir)) { string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir); for (int i = 0; i < fileSystemEntries.Length; i++) { string text = fileSystemEntries[i]; if (System.IO.File.Exists(text)) { System.IO.File.Delete(text); } else { FileHelper.DeleteFolder(text); } } System.IO.Directory.Delete(dir); } }
目錄內容復制
/// <summary> /// 目錄內容復制 /// </summary> /// <param name="srcPath">源目錄</param> /// <param name="aimPath">目標目錄</param> public static void CopyDir(string srcPath, string aimPath) { try { if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) { aimPath += System.IO.Path.DirectorySeparatorChar; } if (!System.IO.Directory.Exists(aimPath)) { System.IO.Directory.CreateDirectory(aimPath); } string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(srcPath); string[] array = fileSystemEntries; for (int i = 0; i < array.Length; i++) { string text = array[i]; if (System.IO.Directory.Exists(text)) { FileHelper.CopyDir(text, aimPath + System.IO.Path.GetFileName(text)); } else { System.IO.File.Copy(text, aimPath + System.IO.Path.GetFileName(text), true); } } } catch (System.Exception ex) { throw new System.Exception(ex.ToString()); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/108749.html
標籤:C#
