FileHelper 通用类

    /// <summary>
    /// 文件通用类
    /// </summary>
    public class FileHelper
    {
         
        
         

        #region 文件相关操作方法

 
        /// <summary> 
        /// 检测指定文件是否存在,如果存在则返回true。
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static bool IsExistFile(string filePath)
        {
            return File.Exists(filePath);
        }

        #region create


        /// <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;
            }
        }

        /// <summary> 
        /// 创建一个文件,并将字节流写入文件。 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        /// <param name="stream">数据流</param> 
        public static bool CreateFile(string filePath, Stream stream)
        {

            bool result = false;
            try
            {
                using (var fs = System.IO.File.Open(filePath, FileMode.OpenOrCreate))
                {
                    int length = 4 * 1024;
                    var buf = new byte[length]; 
                    do{
                        length = stream.Read(buf, 0, length);
                        fs.Write(buf, 0, length);
                    } while (length != 0);

                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        #endregion

        #region Get


        /// <summary> 
        /// 从文件的绝对路径中获取扩展名 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static string GetExtension(string filePath)
        {
            //获取文件的名称 
            FileInfo fi = new FileInfo(filePath);
            return fi.Extension;
        }

        /// <summary> 
        /// 从文件的绝对路径中获取文件名( 包含扩展名 ) 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static string GetFileName(string filePath)
        {
            //获取文件的名称 
            FileInfo fi = new FileInfo(filePath);
            return fi.Name;
        }
        /// <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;
            }
        }


        /// <summary> 
        /// 从文件的绝对路径中获取文件名( 不包含扩展名 ) 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static string GetFileNameNoExtension(string filePath)
        {
            //获取文件的名称
            FileInfo fi = new FileInfo(filePath);
            return fi.Name.Split('.')[0];
        }

        /// <summary> 
        /// 获取文本文件的行数 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static int GetLineCount(string filePath)
        {
            //将文本文件的各行读到一个字符串数组中 
            string[] rows = File.ReadAllLines(filePath);
            //返回行数 
            return rows.Length;
        }

  
        /// <summary> 
        /// 获取一个文件的长度,单位为Byte 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>         
        public static int GetFileSize(string filePath)
        {
            //创建一个文件对象 
            FileInfo fi = new FileInfo(filePath);
            //获取文件的大小 
            return (int)fi.Length;
        }

        /// <summary> 
        /// 将文件读取到缓冲区中 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        public static byte[] GetFileToBytes(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();
            }
        }

        /// <summary> 
        /// 将文件读取到字符串中 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        /// <param name="encoding">字符编码</param> 
        public static string GetFileToString(string filePath, Encoding encoding)
        {
            //创建流读取器 
            StreamReader reader = new StreamReader(filePath, encoding);
            try
            {
                //读取流 
                return reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //关闭流读取器 
                reader.Close();
            }
        }


        #endregion



        #region Insert

        /// <summary> 
        /// 向文本文件中写入内容 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        /// <param name="content">写入的内容</param>         
        public static void WriteText(string filePath, string content)
        {
            //向文件写入内容 
            File.WriteAllText(filePath, content);
        }
        /// <summary> 
        /// 向文本文件的尾部追加内容 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        /// <param name="content">写入的内容</param> 
        public static void WriteAppendText(string filePath, string content)
        {
            File.AppendAllText(filePath, content);
        }


        #endregion
        

        /// <summary> 
        /// 将源文件的内容复制到目标文件中 
        /// </summary> 
        /// <param name="sourceFilePath">源文件的绝对路径</param> 
        /// <param name="destFilePath">目标文件的绝对路径</param> 
        public static void Copy(string sourceFilePath, string destFilePath)
        {
            File.Copy(sourceFilePath, destFilePath, true);
        }

        /// <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);
            }
        }

        

   
        /// <summary> 
        /// 删除指定文件 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param> 
        public static void DeleteFile(string filePath)
        {
            if (IsExistFile(filePath))
            {
                File.Delete(filePath);
            }
        }

        /// <summary> 
        /// 清空文件内容 
        /// </summary> 
        /// <param name="filePath">文件的绝对路径</param>
        /// <param name="iscreatenew"></param> 
        public static void DeleteFile(string filePath,bool iscreatenew)
        {
            //删除文件 
            if (IsExistFile(filePath))
            {
                File.Delete(filePath);
            }
            if (iscreatenew)
            {
                //重新创建该文件 
                CreateFile(filePath);
            }

          
        }


        #endregion


        
        #region 文件目录相关操作方法

        #region Exist

         /// <summary> 
        /// 检测指定目录是否存在,如果存在则返回true。
        /// </summary> 
        /// <param name="directoryPath">目录的绝对路径</param>         
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        /// <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;
            }
        }

        /// <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 = GetFileNames(directoryPath, searchPattern, false);
                //判断指定文件是否存在 
                return fileNames.Length != 0;
            }
            catch (Exception 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 = GetFileNames(directoryPath, searchPattern, true);
                //判断指定文件是否存在 
                return fileNames.Length != 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
       

        #region Create


        /// <summary> 
        /// 创建一个目录 
        /// </summary> 
        /// <param name="directoryPath">目录的绝对路径</param> 
        public static void CreateDirectory(string directoryPath)
        {
            //如果目录不存在则创建该目录 
            if (!IsExistDirectory(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }
        #endregion

        #region Get

        /// <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="directoryPath">指定目录的绝对路径</param> 
        public static void DeleteDirectory(string directoryPath)
        {
            if (IsExistDirectory(directoryPath))
            {
                Directory.Delete(directoryPath, true);
            }
        }

        /// <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


        #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
    }

    /// <summary>    /// 文件通用类    /// </summary>    public class FileHelper    {                          
        #region 文件相关操作方法
         /// <summary>         /// 检测指定文件是否存在,如果存在则返回true。        /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static bool IsExistFile(string filePath)        {            return File.Exists(filePath);        }
        #region create

        /// <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;            }        }
        /// <summary>         /// 创建一个文件,并将字节流写入文件。         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         /// <param name="stream">数据流</param>         public static bool CreateFile(string filePath, Stream stream)        {
            bool result = false;            try            {                using (var fs = System.IO.File.Open(filePath, FileMode.OpenOrCreate))                {                    int length = 4 * 1024;                    var buf = new byte[length];                     do{                        length = stream.Read(buf, 0, length);                        fs.Write(buf, 0, length);                    } while (length != 0);
                    result = true;                }            }            catch (Exception ex)            {                throw ex;            }
            return result;        }
        #endregion
        #region Get

        /// <summary>         /// 从文件的绝对路径中获取扩展名         /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static string GetExtension(string filePath)        {            //获取文件的名称             FileInfo fi = new FileInfo(filePath);            return fi.Extension;        }
        /// <summary>         /// 从文件的绝对路径中获取文件名( 包含扩展名 )         /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static string GetFileName(string filePath)        {            //获取文件的名称             FileInfo fi = new FileInfo(filePath);            return fi.Name;        }        /// <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;            }        }

        /// <summary>         /// 从文件的绝对路径中获取文件名( 不包含扩展名 )         /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static string GetFileNameNoExtension(string filePath)        {            //获取文件的名称            FileInfo fi = new FileInfo(filePath);            return fi.Name.Split('.')[0];        }
        /// <summary>         /// 获取文本文件的行数         /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static int GetLineCount(string filePath)        {            //将文本文件的各行读到一个字符串数组中             string[] rows = File.ReadAllLines(filePath);            //返回行数             return rows.Length;        }
          /// <summary>         /// 获取一个文件的长度,单位为Byte         /// </summary>         /// <param name="filePath">文件的绝对路径</param>                 public static int GetFileSize(string filePath)        {            //创建一个文件对象             FileInfo fi = new FileInfo(filePath);            //获取文件的大小             return (int)fi.Length;        }
        /// <summary>         /// 将文件读取到缓冲区中         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         public static byte[] GetFileToBytes(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();            }        }
        /// <summary>         /// 将文件读取到字符串中         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         /// <param name="encoding">字符编码</param>         public static string GetFileToString(string filePath, Encoding encoding)        {            //创建流读取器             StreamReader reader = new StreamReader(filePath, encoding);            try            {                //读取流                 return reader.ReadToEnd();            }            catch (Exception ex)            {                throw ex;            }            finally            {                //关闭流读取器                 reader.Close();            }        }

        #endregion


        #region Insert
        /// <summary>         /// 向文本文件中写入内容         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         /// <param name="content">写入的内容</param>                 public static void WriteText(string filePath, string content)        {            //向文件写入内容             File.WriteAllText(filePath, content);        }        /// <summary>         /// 向文本文件的尾部追加内容         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         /// <param name="content">写入的内容</param>         public static void WriteAppendText(string filePath, string content)        {            File.AppendAllText(filePath, content);        }

        #endregion        
        /// <summary>         /// 将源文件的内容复制到目标文件中         /// </summary>         /// <param name="sourceFilePath">源文件的绝对路径</param>         /// <param name="destFilePath">目标文件的绝对路径</param>         public static void Copy(string sourceFilePath, string destFilePath)        {            File.Copy(sourceFilePath, destFilePath, true);        }
        /// <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);            }        }
        
           /// <summary>         /// 删除指定文件         /// </summary>         /// <param name="filePath">文件的绝对路径</param>         public static void DeleteFile(string filePath)        {            if (IsExistFile(filePath))            {                File.Delete(filePath);            }        }
        /// <summary>         /// 清空文件内容         /// </summary>         /// <param name="filePath">文件的绝对路径</param>        /// <param name="iscreatenew"></param>         public static void DeleteFile(string filePath,bool iscreatenew)        {            //删除文件             if (IsExistFile(filePath))            {                File.Delete(filePath);            }            if (iscreatenew)            {                //重新创建该文件                 CreateFile(filePath);            }
                  }

        #endregion

                #region 文件目录相关操作方法
        #region Exist
         /// <summary>         /// 检测指定目录是否存在,如果存在则返回true。        /// </summary>         /// <param name="directoryPath">目录的绝对路径</param>                 public static bool IsExistDirectory(string directoryPath)        {            return Directory.Exists(directoryPath);        }        /// <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;            }        }
        /// <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 = GetFileNames(directoryPath, searchPattern, false);                //判断指定文件是否存在                 return fileNames.Length != 0;            }            catch (Exception 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 = GetFileNames(directoryPath, searchPattern, true);                //判断指定文件是否存在                 return fileNames.Length != 0;            }            catch (Exception ex)            {                throw ex;            }        }
        #endregion       
        #region Create

        /// <summary>         /// 创建一个目录         /// </summary>         /// <param name="directoryPath">目录的绝对路径</param>         public static void CreateDirectory(string directoryPath)        {            //如果目录不存在则创建该目录             if (!IsExistDirectory(directoryPath))            {                Directory.CreateDirectory(directoryPath);            }        }        #endregion
        #region Get
        /// <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="directoryPath">指定目录的绝对路径</param>         public static void DeleteDirectory(string directoryPath)        {            if (IsExistDirectory(directoryPath))            {                Directory.Delete(directoryPath, true);            }        }
        /// <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

        #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    }

Top
收藏
关注
评论
原文地址:https://www.cnblogs.com/for917157ever/p/15137695.html