主营业务:网站制作 网站优化 小程序制作 石家庄网站制作网站建设有限公司欢迎您!
石家庄闪云网络科技有限公司
客服热线15383239821
如何让自己的网站更有质量?

asp.net常用的文件与文件夹操作类

发布时间:2013/10/30 16:24:44

.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;
                }
            }
      &n

相关新闻推荐

在线客服 : 服务热线:15383239821 电子邮箱: 27535611@qq.com

公司地址:石家庄市新华区九中街江西大厦4062

备案号:冀ICP备2022000585号-4