檔案以及檔案夾幫助類(FileHelper)
代碼:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FileHelperDemo { /// <summary> /// 檔案操作幫助類 /// </summary> public class FileUtilHelper { #region 檢測指定目錄是否存在 /// <summary> /// 檢測指定目錄是否存在,如果存在則回傳true, /// </summary> /// <param name="directoryPath">目錄的絕對路徑</param> public static bool IsExistDirectory(string directoryPath) { return Directory.Exists(directoryPath); } #endregion #region 檢測指定檔案是否存在 /// <summary> /// 檢測指定檔案是否存在,如果存在則回傳true, /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static bool IsExistFile(string filePath) { return File.Exists(filePath); } #endregion #region 檢測指定目錄是否為空 /// <summary> /// 檢測指定目錄是否為空 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> public static bool IsEmptyDirectory(string directoryPath) { try { //判斷是否存在檔案 string[] fileNames = GetFileNames(directoryPath); if (fileNames.Length > 0) { return false; } //判斷是否存在檔案夾 string[] directoryNames = GetDirectories(directoryPath); if (directoryNames.Length > 0) { return false; } return true; } catch (Exception ex) { throw ex; } } #endregion #region 檢測指定目錄中是否存在指定的檔案 /// <summary> /// 檢測指定目錄中是否存在指定的檔案,若要搜索子目錄請使用多載方法. /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> /// <param name="searchPattern">模式字串,"*"代表0或N個字符,"?"代表1個字符, /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml檔案,</param> public static bool IsContainsFile(string directoryPath, string searchPattern) { try { //獲取指定的檔案串列 string[] fileNames = GetFileNames1(directoryPath, searchPattern, false); //判斷指定檔案是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw ex; } } public static string[] GetFileNames1(string directoryPath, string searchPattern, bool isSearchChild) { //如果目錄不存在,則拋出例外 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } /// <summary> /// 檢測指定目錄中是否存在指定的檔案 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> /// <param name="searchPattern">模式字串,"*"代表0或N個字符,"?"代表1個字符, /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml檔案,</param> /// <param name="isSearchChild">是否搜索子目錄</param> public static bool IsContainsFile(string directoryPath, string searchPattern, bool isSearchChild) { try { //獲取指定的檔案串列 string[] fileNames = GetFileNames2(directoryPath, searchPattern, true); //判斷指定檔案是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw ex; } } public static string[] GetFileNames2(string directoryPath, string searchPattern, bool isSearchChild) { //如果目錄不存在,則拋出例外 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 創建一個目錄 /// <summary> /// 創建一個目錄 /// </summary> /// <param name="directoryPath">目錄的絕對路徑</param> public static void CreateDirectory(string directoryPath) { //如果目錄不存在則創建該目錄 if (!IsExistDirectory(directoryPath)) { Directory.CreateDirectory(directoryPath); } } #endregion #region 創建一個檔案 /// <summary> /// 創建一個檔案 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static void CreateFile(string filePath) { try { //如果檔案不存在則創建該檔案 if (!IsExistFile(filePath)) { //創建一個FileInfo物件 FileInfo file = new FileInfo(filePath); //創建檔案 FileStream fs = file.Create(); //關閉檔案流 fs.Close(); } } catch (Exception ex) { throw ex; } } /// <summary> /// 創建一個檔案,并將位元組流寫入檔案, /// </summary> /// <param name="filePath">檔案的絕對路徑</param> /// <param name="buffer">二進制流資料</param> public static void CreateFile(string filePath, byte[] buffer) { try { //如果檔案不存在則創建該檔案 if (!IsExistFile(filePath)) { //創建一個FileInfo物件 FileInfo file = new FileInfo(filePath); //創建檔案 FileStream fs = file.Create(); //寫入二進制流 fs.Write(buffer, 0, buffer.Length); //關閉檔案流 fs.Close(); } } catch (Exception ex) { throw ex; } } #endregion #region 獲取文本檔案的行數 /// <summary> /// 獲取文本檔案的行數 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static int GetLineCount(string filePath) { //將文本檔案的各行讀到一個字串陣列中 string[] rows = File.ReadAllLines(filePath); //回傳行數 return rows.Length; } #endregion #region 獲取一個檔案的長度 /// <summary> /// 獲取一個檔案的長度,單位為Byte /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static int GetFileSize(string filePath) { //創建一個檔案物件 FileInfo fi = new FileInfo(filePath); //獲取檔案的大小 return (int)fi.Length; } #endregion #region 獲取指定目錄中的檔案串列 /// <summary> /// 獲取指定目錄中所有檔案串列 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> public static string[] GetFileNames(string directoryPath) { //如果目錄不存在,則拋出例外 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } //獲取檔案串列 return Directory.GetFiles(directoryPath); } /// <summary> /// 獲取指定目錄及子目錄中所有檔案串列 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> /// <param name="searchPattern">模式字串,"*"代表0或N個字符,"?"代表1個字符, /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml檔案,</param> /// <param name="isSearchChild">是否搜索子目錄</param> public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild) { //如果目錄不存在,則拋出例外 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 獲取指定目錄中的子目錄串列 /// <summary> /// 獲取指定目錄中所有子目錄串列,若要搜索嵌套的子目錄串列,請使用多載方法. /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> public static string[] GetDirectories(string directoryPath) { try { return Directory.GetDirectories(directoryPath); } catch (IOException ex) { throw ex; } } /// <summary> /// 獲取指定目錄及子目錄中所有子目錄串列 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> /// <param name="searchPattern">模式字串,"*"代表0或N個字符,"?"代表1個字符, /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml檔案,</param> /// <param name="isSearchChild">是否搜索子目錄</param> public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild) { try { if (isSearchChild) { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 向文本檔案寫入內容 /// <summary> /// 向文本檔案中寫入內容 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> /// <param name="content">寫入的內容</param> public static void WriteText(string filePath, string content) { //向檔案寫入內容 File.WriteAllText(filePath, content); } #endregion #region 向文本檔案的尾部追加內容 /// <summary> /// 向文本檔案的尾部追加內容 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> /// <param name="content">寫入的內容</param> public static void AppendText(string filePath, string content) { File.AppendAllText(filePath, content); } #endregion #region 將現有檔案的內容復制到新檔案中 /// <summary> /// 將源檔案的內容復制到目標檔案中 /// </summary> /// <param name="sourceFilePath">源檔案的絕對路徑</param> /// <param name="destFilePath">目標檔案的絕對路徑</param> public static void Copy(string sourceFilePath, string destFilePath) { File.Copy(sourceFilePath, destFilePath, true); } #endregion #region 將檔案移動到指定目錄 /// <summary> /// 將檔案移動到指定目錄 /// </summary> /// <param name="sourceFilePath">需要移動的源檔案的絕對路徑</param> /// <param name="descDirectoryPath">移動到的目錄的絕對路徑</param> public static void Move(string sourceFilePath, string descDirectoryPath) { //獲取源檔案的名稱 string sourceFileName = GetFileName(sourceFilePath); if (IsExistDirectory(descDirectoryPath)) { //如果目標中存在同名檔案,則洗掉 if (IsExistFile(descDirectoryPath + "\\" + sourceFileName)) { DeleteFile(descDirectoryPath + "\\" + sourceFileName); } //將檔案移動到指定目錄 File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName); } } #endregion #region 從檔案的絕對路徑中獲取檔案名( 包含擴展名 ) /// <summary> /// 從檔案的絕對路徑中獲取檔案名( 包含擴展名 ) /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static string GetFileName(string filePath) { //獲取檔案的名稱 FileInfo fi = new FileInfo(filePath); return fi.Name; } #endregion #region 從檔案的絕對路徑中獲取檔案名( 不包含擴展名 ) /// <summary> /// 從檔案的絕對路徑中獲取檔案名( 不包含擴展名 ) /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static string GetFileNameNoExtension(string filePath) { //獲取檔案的名稱 FileInfo fi = new FileInfo(filePath); return fi.Name.Split('.')[0]; } #endregion #region 從檔案的絕對路徑中獲取擴展名 /// <summary> /// 從檔案的絕對路徑中獲取擴展名 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static string GetExtension(string filePath) { //獲取檔案的名稱 FileInfo fi = new FileInfo(filePath); return fi.Extension; } #endregion #region 將檔案讀取到緩沖區中 /// <summary> /// 將檔案讀取到緩沖區中 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static byte[] FileToBytes(string filePath) { //獲取檔案的大小 int fileSize = GetFileSize(filePath); //創建一個臨時緩沖區 byte[] buffer = new byte[fileSize]; //創建一個檔案流 FileInfo fi = new FileInfo(filePath); FileStream fs = fi.Open(FileMode.Open); try { //將檔案流讀入緩沖區 fs.Read(buffer, 0, fileSize); return buffer; } catch (IOException ex) { throw ex; } finally { //關閉檔案流 fs.Close(); } } #endregion #region 將檔案讀取到字串中 /// <summary> /// 將檔案讀取到字串中 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> /// <param name="encoding">字符編碼</param> public static string FileToString(string filePath, Encoding encoding) { //創建流讀取器 StreamReader reader = new StreamReader(filePath, encoding); try { //讀取流 return reader.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { //關閉流讀取器 reader.Close(); } } #endregion #region 清空指定目錄 /// <summary> /// 清空指定目錄下所有檔案及子目錄,但該目錄依然保存. /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> public static void ClearDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { //洗掉目錄中所有的檔案 string[] fileNames = GetFileNames(directoryPath); for (int i = 0; i < fileNames.Length; i++) { DeleteFile(fileNames[i]); } //洗掉目錄中所有的子目錄 string[] directoryNames = GetDirectories(directoryPath); for (int i = 0; i < directoryNames.Length; i++) { DeleteDirectory(directoryNames[i]); } } } #endregion #region 清空檔案內容 /// <summary> /// 清空檔案內容 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static void ClearFile(string filePath) { //洗掉檔案 File.Delete(filePath); //重新創建該檔案 CreateFile(filePath); } #endregion #region 洗掉指定檔案 /// <summary> /// 洗掉指定檔案 /// </summary> /// <param name="filePath">檔案的絕對路徑</param> public static void DeleteFile(string filePath) { if (IsExistFile(filePath)) { File.Delete(filePath); } } #endregion #region 洗掉指定目錄 /// <summary> /// 洗掉指定目錄及其所有子目錄 /// </summary> /// <param name="directoryPath">指定目錄的絕對路徑</param> public static void DeleteDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { Directory.Delete(directoryPath, true); } } #endregion #region 將位元組(B)轉換成兆(M),并保留兩位小數 /// <summary> /// 將位元組(B)轉換成兆(M),并保留兩位小數 /// </summary> public static string ConvertByteToMB(int byteLength) { return Math.Round((double)byteLength / (1024 * 1024), 2).ToString() + "M"; } #endregion #region 去除檔案名中的特殊字符 public static string ReplaceBadCharOfFileName(string fileName) { fileName = fileName.Replace("\\", string.Empty); fileName = fileName.Replace("/", string.Empty); fileName = fileName.Replace(":", string.Empty); fileName = fileName.Replace("*", string.Empty); fileName = fileName.Replace("?", string.Empty); fileName = fileName.Replace("\"", string.Empty); fileName = fileName.Replace("<", string.Empty); fileName = fileName.Replace(">", string.Empty); fileName = fileName.Replace("|", string.Empty); fileName = fileName.Replace(" ", string.Empty); return fileName.ToString(); } #endregion } }FileHelper
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/285344.html
標籤:C#
下一篇:C#正則運算式
