asp。net中常用的文件操作类
生活随笔
收集整理的這篇文章主要介紹了
asp。net中常用的文件操作类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
**
文件操作類
**/
#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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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 創(chuàng)建一個(gè)目錄
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)目錄
??????? /// </summary>
??????? /// <param name="directoryPath">目錄的絕對路徑</param>
??????? public static void CreateDirectory( string directoryPath )
??????? {
??????????? //如果目錄不存在則創(chuàng)建該目錄
??????????? if ( !IsExistDirectory( directoryPath ) )
??????????? {
??????????????? Directory.CreateDirectory( directoryPath );
??????????? }
??????? }
??????? #endregion
??????? #region 創(chuàng)建一個(gè)文件
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)文件。
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static void CreateFile( string filePath )
??????? {
??????????? try
??????????? {
??????????????? //如果文件不存在則創(chuàng)建該文件
??????????????? if ( !IsExistFile( filePath ) )
??????????????? {
??????????????????? //創(chuàng)建一個(gè)FileInfo對象
??????????????????? FileInfo file = new FileInfo( filePath );
??????????????????? //創(chuàng)建文件
??????????????????? FileStream fs = file.Create();
??????????????????? //關(guān)閉文件流
??????????????????? fs.Close();
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????? }
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)文件,并將字節(jié)流寫入文件。
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="buffer">二進(jìn)制流數(shù)據(jù)</param>
??????? public static void CreateFile( string filePath, byte[] buffer )
??????? {
??????????? try
??????????? {
??????????????? //如果文件不存在則創(chuàng)建該文件
??????????????? if ( !IsExistFile( filePath ) )
??????????????? {
??????????????????? //創(chuàng)建一個(gè)FileInfo對象
??????????????????? FileInfo file = new FileInfo( filePath );
??????????????????? //創(chuàng)建文件
??????????????????? FileStream fs = file.Create();
??????????????????? //寫入二進(jìn)制流
??????????????????? fs.Write( buffer, 0, buffer.Length );
??????????????????? //關(guān)閉文件流
??????????????????? fs.Close();
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????? }
??????? #endregion
??????? #region 獲取文本文件的行數(shù)
??????? /// <summary>
??????? /// 獲取文本文件的行數(shù)
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static int GetLineCount( string filePath )
??????? {
??????????? //將文本文件的各行讀到一個(gè)字符串?dāng)?shù)組中
??????????? string[] rows = File.ReadAllLines( filePath );
??????????? //返回行數(shù)
??????????? return rows.Length;
??????? }
??????? #endregion
??????? #region 獲取一個(gè)文件的長度
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為Byte
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static int GetFileSize( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? FileInfo fi = new FileInfo( filePath );
??????????? //獲取文件的大小
??????????? return (int)fi.Length;
??????? }
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為KB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByKB( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? FileInfo fi = new FileInfo( filePath );???????????
??????????? //獲取文件的大小
??????????? return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / 1024 , 1 );
??????? }
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為MB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByMB( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? 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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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 向文本文件寫入內(nèi)容
??????? /// <summary>
??????? /// 向文本文件中寫入內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="content">寫入的內(nèi)容</param>????????
??????? public static void WriteText( string filePath, string content )
??????? {
??????????? //向文件寫入內(nèi)容
??????????? File.WriteAllText( filePath, content );
??????? }
??????? #endregion
??????? #region 向文本文件的尾部追加內(nèi)容
??????? /// <summary>
??????? /// 向文本文件的尾部追加內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="content">寫入的內(nèi)容</param>
??????? public static void AppendText( string filePath, string content )
??????? {
??????????? File.AppendAllText( filePath, content );
??????? }
??????? #endregion
??????? #region 將現(xiàn)有文件的內(nèi)容復(fù)制到新文件中
??????? /// <summary>
??????? /// 將源文件的內(nèi)容復(fù)制到目標(biāo)文件中
??????? /// </summary>
??????? /// <param name="sourceFilePath">源文件的絕對路徑</param>
??????? /// <param name="destFilePath">目標(biāo)文件的絕對路徑</param>
??????? public static void Copy( string sourceFilePath, string destFilePath )
??????? {
??????????? File.Copy( sourceFilePath, destFilePath, true );
??????? }
??????? #endregion
??????? #region 將文件移動(dòng)到指定目錄
??????? /// <summary>
??????? /// 將文件移動(dòng)到指定目錄
??????? /// </summary>
??????? /// <param name="sourceFilePath">需要移動(dòng)的源文件的絕對路徑</param>
??????? /// <param name="descDirectoryPath">移動(dòng)到的目錄的絕對路徑</param>
??????? public static void Move( string sourceFilePath,string descDirectoryPath )
??????? {????????????
??????????? //獲取源文件的名稱
??????????? string sourceFileName = GetFileName( sourceFilePath );???????????
??????????? if ( IsExistDirectory( descDirectoryPath ) )
??????????? {
??????????????? //如果目標(biāo)中存在同名文件,則刪除
??????????????? if ( IsExistFile( descDirectoryPath + "\\" + sourceFileName ) )
??????????????? {
??????????????????? DeleteFile( descDirectoryPath + "\\" + sourceFileName );
??????????????? }
??????????????? //將文件移動(dòng)到指定目錄
??????????????? File.Move( sourceFilePath, descDirectoryPath + "\\" + sourceFileName );
??????????? }
??????? }
??????? #endregion
??????? #region 將流讀取到緩沖區(qū)中
??????? /// <summary>
??????? /// 將流讀取到緩沖區(qū)中
??????? /// </summary>
??????? /// <param name="stream">原始流</param>
??????? public static byte[] StreamToBytes( Stream stream )
??????? {
??????????? try
??????????? {
??????????????? //創(chuàng)建緩沖區(qū)
??????????????? 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
??????????? {
??????????????? //關(guān)閉流
??????????????? stream.Close();
??????????? }
??????? }
??????? #endregion
??????? #region 將文件讀取到緩沖區(qū)中
??????? /// <summary>
??????? /// 將文件讀取到緩沖區(qū)中
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static byte[] FileToBytes( string filePath )
??????? {
??????????? //獲取文件的大小?
??????????? int fileSize = GetFileSize( filePath );
??????????? //創(chuàng)建一個(gè)臨時(shí)緩沖區(qū)
??????????? byte[] buffer = new byte[fileSize];
??????????? //創(chuàng)建一個(gè)文件流
??????????? FileInfo fi = new FileInfo( filePath );
??????????? FileStream fs = fi.Open( FileMode.Open );
??????????? try
??????????? {
??????????????? //將文件流讀入緩沖區(qū)
??????????????? fs.Read( buffer, 0, fileSize );
??????????????? return buffer;
??????????? }
??????????? catch ( IOException ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關(guān)閉文件流
??????????????? 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 )
??????? {
??????????? //創(chuàng)建流讀取器
??????????? StreamReader reader = new StreamReader( filePath, encoding );
??????????? try
??????????? {
??????????????? //讀取流
??????????????? return reader.ReadToEnd();
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關(guān)閉流讀取器
??????????????? reader.Close();
??????????? }
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取文件名( 包含擴(kuò)展名 )
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取文件名( 包含擴(kuò)展名 )
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static string GetFileName( string filePath )
??????? {
??????????? //獲取文件的名稱
??????????? FileInfo fi = new FileInfo( filePath );
??????????? return fi.Name;
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取文件名( 不包含擴(kuò)展名 )
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取文件名( 不包含擴(kuò)展名 )
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static string GetFileNameNoExtension( string filePath )
??????? {
??????????? //獲取文件的名稱
??????????? FileInfo fi = new FileInfo( filePath );
??????????? return fi.Name.Split( '.' )[0];
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取擴(kuò)展名
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取擴(kuò)展名
??????? /// </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 清空文件內(nèi)容
??????? /// <summary>
??????? /// 清空文件內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static void ClearFile( string filePath )
??????? {
??????????? //刪除文件
??????????? File.Delete( filePath );
??????????? //重新創(chuàng)建該文件
??????????? 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 引用命名空間
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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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 創(chuàng)建一個(gè)目錄
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)目錄
??????? /// </summary>
??????? /// <param name="directoryPath">目錄的絕對路徑</param>
??????? public static void CreateDirectory( string directoryPath )
??????? {
??????????? //如果目錄不存在則創(chuàng)建該目錄
??????????? if ( !IsExistDirectory( directoryPath ) )
??????????? {
??????????????? Directory.CreateDirectory( directoryPath );
??????????? }
??????? }
??????? #endregion
??????? #region 創(chuàng)建一個(gè)文件
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)文件。
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static void CreateFile( string filePath )
??????? {
??????????? try
??????????? {
??????????????? //如果文件不存在則創(chuàng)建該文件
??????????????? if ( !IsExistFile( filePath ) )
??????????????? {
??????????????????? //創(chuàng)建一個(gè)FileInfo對象
??????????????????? FileInfo file = new FileInfo( filePath );
??????????????????? //創(chuàng)建文件
??????????????????? FileStream fs = file.Create();
??????????????????? //關(guān)閉文件流
??????????????????? fs.Close();
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????? }
??????? /// <summary>
??????? /// 創(chuàng)建一個(gè)文件,并將字節(jié)流寫入文件。
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="buffer">二進(jìn)制流數(shù)據(jù)</param>
??????? public static void CreateFile( string filePath, byte[] buffer )
??????? {
??????????? try
??????????? {
??????????????? //如果文件不存在則創(chuàng)建該文件
??????????????? if ( !IsExistFile( filePath ) )
??????????????? {
??????????????????? //創(chuàng)建一個(gè)FileInfo對象
??????????????????? FileInfo file = new FileInfo( filePath );
??????????????????? //創(chuàng)建文件
??????????????????? FileStream fs = file.Create();
??????????????????? //寫入二進(jìn)制流
??????????????????? fs.Write( buffer, 0, buffer.Length );
??????????????????? //關(guān)閉文件流
??????????????????? fs.Close();
??????????????? }
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????? }
??????? #endregion
??????? #region 獲取文本文件的行數(shù)
??????? /// <summary>
??????? /// 獲取文本文件的行數(shù)
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static int GetLineCount( string filePath )
??????? {
??????????? //將文本文件的各行讀到一個(gè)字符串?dāng)?shù)組中
??????????? string[] rows = File.ReadAllLines( filePath );
??????????? //返回行數(shù)
??????????? return rows.Length;
??????? }
??????? #endregion
??????? #region 獲取一個(gè)文件的長度
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為Byte
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static int GetFileSize( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? FileInfo fi = new FileInfo( filePath );
??????????? //獲取文件的大小
??????????? return (int)fi.Length;
??????? }
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為KB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByKB( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? FileInfo fi = new FileInfo( filePath );???????????
??????????? //獲取文件的大小
??????????? return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / 1024 , 1 );
??????? }
??????? /// <summary>
??????? /// 獲取一個(gè)文件的長度,單位為MB
??????? /// </summary>
??????? /// <param name="filePath">文件的路徑</param>????????
??????? public static double GetFileSizeByMB( string filePath )
??????? {
??????????? //創(chuàng)建一個(gè)文件對象
??????????? 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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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個(gè)字符,"?"代表1個(gè)字符。
??????? /// 范例:"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 向文本文件寫入內(nèi)容
??????? /// <summary>
??????? /// 向文本文件中寫入內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="content">寫入的內(nèi)容</param>????????
??????? public static void WriteText( string filePath, string content )
??????? {
??????????? //向文件寫入內(nèi)容
??????????? File.WriteAllText( filePath, content );
??????? }
??????? #endregion
??????? #region 向文本文件的尾部追加內(nèi)容
??????? /// <summary>
??????? /// 向文本文件的尾部追加內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? /// <param name="content">寫入的內(nèi)容</param>
??????? public static void AppendText( string filePath, string content )
??????? {
??????????? File.AppendAllText( filePath, content );
??????? }
??????? #endregion
??????? #region 將現(xiàn)有文件的內(nèi)容復(fù)制到新文件中
??????? /// <summary>
??????? /// 將源文件的內(nèi)容復(fù)制到目標(biāo)文件中
??????? /// </summary>
??????? /// <param name="sourceFilePath">源文件的絕對路徑</param>
??????? /// <param name="destFilePath">目標(biāo)文件的絕對路徑</param>
??????? public static void Copy( string sourceFilePath, string destFilePath )
??????? {
??????????? File.Copy( sourceFilePath, destFilePath, true );
??????? }
??????? #endregion
??????? #region 將文件移動(dòng)到指定目錄
??????? /// <summary>
??????? /// 將文件移動(dòng)到指定目錄
??????? /// </summary>
??????? /// <param name="sourceFilePath">需要移動(dòng)的源文件的絕對路徑</param>
??????? /// <param name="descDirectoryPath">移動(dòng)到的目錄的絕對路徑</param>
??????? public static void Move( string sourceFilePath,string descDirectoryPath )
??????? {????????????
??????????? //獲取源文件的名稱
??????????? string sourceFileName = GetFileName( sourceFilePath );???????????
??????????? if ( IsExistDirectory( descDirectoryPath ) )
??????????? {
??????????????? //如果目標(biāo)中存在同名文件,則刪除
??????????????? if ( IsExistFile( descDirectoryPath + "\\" + sourceFileName ) )
??????????????? {
??????????????????? DeleteFile( descDirectoryPath + "\\" + sourceFileName );
??????????????? }
??????????????? //將文件移動(dòng)到指定目錄
??????????????? File.Move( sourceFilePath, descDirectoryPath + "\\" + sourceFileName );
??????????? }
??????? }
??????? #endregion
??????? #region 將流讀取到緩沖區(qū)中
??????? /// <summary>
??????? /// 將流讀取到緩沖區(qū)中
??????? /// </summary>
??????? /// <param name="stream">原始流</param>
??????? public static byte[] StreamToBytes( Stream stream )
??????? {
??????????? try
??????????? {
??????????????? //創(chuàng)建緩沖區(qū)
??????????????? 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
??????????? {
??????????????? //關(guān)閉流
??????????????? stream.Close();
??????????? }
??????? }
??????? #endregion
??????? #region 將文件讀取到緩沖區(qū)中
??????? /// <summary>
??????? /// 將文件讀取到緩沖區(qū)中
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static byte[] FileToBytes( string filePath )
??????? {
??????????? //獲取文件的大小?
??????????? int fileSize = GetFileSize( filePath );
??????????? //創(chuàng)建一個(gè)臨時(shí)緩沖區(qū)
??????????? byte[] buffer = new byte[fileSize];
??????????? //創(chuàng)建一個(gè)文件流
??????????? FileInfo fi = new FileInfo( filePath );
??????????? FileStream fs = fi.Open( FileMode.Open );
??????????? try
??????????? {
??????????????? //將文件流讀入緩沖區(qū)
??????????????? fs.Read( buffer, 0, fileSize );
??????????????? return buffer;
??????????? }
??????????? catch ( IOException ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關(guān)閉文件流
??????????????? 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 )
??????? {
??????????? //創(chuàng)建流讀取器
??????????? StreamReader reader = new StreamReader( filePath, encoding );
??????????? try
??????????? {
??????????????? //讀取流
??????????????? return reader.ReadToEnd();
??????????? }
??????????? catch ( Exception ex )
??????????? {
??????????????? LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
??????????????? throw ex;
??????????? }
??????????? finally
??????????? {
??????????????? //關(guān)閉流讀取器
??????????????? reader.Close();
??????????? }
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取文件名( 包含擴(kuò)展名 )
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取文件名( 包含擴(kuò)展名 )
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static string GetFileName( string filePath )
??????? {
??????????? //獲取文件的名稱
??????????? FileInfo fi = new FileInfo( filePath );
??????????? return fi.Name;
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取文件名( 不包含擴(kuò)展名 )
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取文件名( 不包含擴(kuò)展名 )
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>????????
??????? public static string GetFileNameNoExtension( string filePath )
??????? {
??????????? //獲取文件的名稱
??????????? FileInfo fi = new FileInfo( filePath );
??????????? return fi.Name.Split( '.' )[0];
??????? }
??????? #endregion
??????? #region 從文件的絕對路徑中獲取擴(kuò)展名
??????? /// <summary>
??????? /// 從文件的絕對路徑中獲取擴(kuò)展名
??????? /// </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 清空文件內(nèi)容
??????? /// <summary>
??????? /// 清空文件內(nèi)容
??????? /// </summary>
??????? /// <param name="filePath">文件的絕對路徑</param>
??????? public static void ClearFile( string filePath )
??????? {
??????????? //刪除文件
??????????? File.Delete( filePath );
??????????? //重新創(chuàng)建該文件
??????????? 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
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/eart/archive/2011/05/24/2056020.html
總結(jié)
以上是生活随笔為你收集整理的asp。net中常用的文件操作类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 变量
- 下一篇: web前端-HTML 媒体插件 022