日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

asp。net中常用的文件操作类

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp。net中常用的文件操作类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**
文件操作類
**/
#region 引用命名空間
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion
namespace CommonUtilities
{
??? /// <summary>
??? /// 文件操作類
??? /// </summary>
??? public class FileHelper
??? {
??????? #region 檢測指定目錄是否存在
??????? /// <summary>
??????? /// 檢測指定目錄是否存在
??????? /// </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 )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? return true;
??????????? }
??????? }
??????? #endregion
??????? #region 檢測指定目錄中是否存在指定的文件
??????? /// <summary>
??????? /// 檢測指定目錄中是否存在指定的文件,若要搜索子目錄請使用重載方法.
??????? /// </summary>
??????? /// <param name="directoryPath">指定目錄的絕對路徑</param>
??????? /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
??????? /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>????????
??????? public static bool Contains( string directoryPath, string searchPattern )
??????? {
??????????? try
??????????? {
??????????????? //獲取指定的文件列表
??????????????? string[] fileNames = GetFileNames( directoryPath, searchPattern, false );
??????????????? //判斷指定文件是否存在
??????????????? if ( fileNames.Length == 0 )
??????????????? {
??????????????????? return false;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return true;
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? return false;
??????????? }
??????? }
??????? /// <summary>
??????? /// 檢測指定目錄中是否存在指定的文件
??????? /// </summary>
??????? /// <param name="directoryPath">指定目錄的絕對路徑</param>
??????? /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
??????? /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>?
??????? /// <param name="isSearchChild">是否搜索子目錄</param>
??????? public static bool Contains( string directoryPath, string searchPattern, bool isSearchChild )
??????? {
??????????? try
??????????? {
??????????????? //獲取指定的文件列表
??????????????? string[] fileNames = GetFileNames( directoryPath, searchPattern, true );
??????????????? //判斷指定文件是否存在
??????????????? if ( fileNames.Length == 0 )
??????????????? {
??????????????????? return false;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return true;
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? return false;
??????????? }
??????? }
??????? #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 )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? 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 )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? 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;
??????? }
??????? /// <summary>
??????? /// 獲取一個文件的長度,單位為KB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByKB( string filePath )
??????? {
??????????? //創建一個文件對象
??????????? FileInfo fi = new FileInfo( filePath );???????????
??????????? //獲取文件的大小
??????????? return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / 1024 , 1 );
??????? }
??????? /// <summary>
??????? /// 獲取一個文件的長度,單位為MB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByMB( string filePath )
??????? {
??????????? //創建一個文件對象
??????????? FileInfo fi = new FileInfo( filePath );
??????????? //獲取文件的大小
??????????? return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / 1024 / 1024 , 1 );
??????? }
??????? #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="stream">原始流</param>
??????? public static byte[] StreamToBytes( Stream stream )
??????? {
??????????? try
??????????? {
??????????????? //創建緩沖區
??????????????? byte[] buffer = new byte[stream.Length];
??????????????? //讀取流
??????????????? stream.Read( buffer, 0, ConvertHelper.ToInt32( stream.Length ) );
??????????????? //返回流
??????????????? return buffer;
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關閉流
??????????????? stream.Close();
??????????? }
??????? }
??????? #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 )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關閉文件流
??????????????? fs.Close();
??????????? }
??????? }
??????? #endregion???????
??????? #region 將文件讀取到字符串中
??????? /// <summary>
??????? /// 將文件讀取到字符串中
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static string FileToString( string filePath )
??????? {
??????????? return FileToString( filePath, BaseInfo.DefaultEncoding );
??????? }
??????? /// <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 )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關閉流讀取器
??????????????? reader.Close();
??????????? }
??????? }
??????? #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="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
??? }
}

轉載于:https://www.cnblogs.com/eart/archive/2011/05/24/2056020.html

總結

以上是生活随笔為你收集整理的asp。net中常用的文件操作类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。