服務(wù)熱線(xiàn)
153 8323 9821
.net常用的文件與文件夾操作類(lèi)
#region 引用命名空間
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion
namespace CommonUtilities
{
/// <summary>
/// 文件操作類(lèi)
/// </summary>
public class FileHelper
{
#region 檢測(cè)指定目錄是否存在
/// <summary>
/// 檢測(cè)指定目錄是否存在
/// </summary>
/// <param name="directoryPath">目錄的絕對(duì)路徑</param>
public static bool IsExistDirectory( string directoryPath )
{
return Directory.Exists( directoryPath );
}
#endregion
#region 檢測(cè)指定文件是否存在
/// <summary>
/// 檢測(cè)指定文件是否存在,如果存在則返回true。
/// </summary>
/// <param name="filePath">文件的絕對(duì)路徑</param>
public static bool IsExistFile( string filePath )
{
return File.Exists( filePath );
}
#endregion
#region 檢測(cè)指定目錄是否為空
/// <summary>
/// 檢測(cè)指定目錄是否為空
/// </summary>
/// <param name="directoryPath">指定目錄的絕對(duì)路徑</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 檢測(cè)指定目錄中是否存在指定的文件
/// <summary>
/// 檢測(cè)指定目錄中是否存在指定的文件,若要搜索子目錄請(qǐng)使用重載方法.
/// </summary>
/// <param name="directoryPath">指定目錄的絕對(duì)路徑</param>
/// <param name="searchPattern">模式字符串,"×"代表0或N個(gè)字符,"?"代表1個(gè)字符。
/// 范例:"Log×.xml"表示搜索所有以Log開(kāi)頭的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;
}
}
&n