打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#中对文件File常用操作方法的工具类

场景

C#中File类的常用读取与写入文件方法的使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

获取文件的扩展名

        /// <summary>        /// 获取文件的扩展名        /// </summary>        /// <param name="filename">完整文件名</param>        /// <returns>返回扩展名</returns>  public static string GetPostfixStr(string filename)  {   int num = filename.LastIndexOf(".");   int length = filename.Length;   return filename.Substring(num, length - num);  }

 

读取文件内容

  

      /// <summary>        /// 读取文件内容        /// </summary>        /// <param name="path">要读取的文件路径</param>        /// <returns>返回文件内容</returns>  public static string ReadFile(string path)  {   string result;   if (!System.IO.File.Exists(path))   {    result = "不存在相应的目录";   }   else   {    System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);    System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);    result = streamReader.ReadToEnd();    streamReader.Close();    streamReader.Dispose();   }   return result;  }

 

指定编码格式读取文件内容

   

     /// <summary>        /// 读取文件内容        /// </summary>        /// <param name="path">要读取的文件路径</param>        /// <param name="encoding">编码格式</param>        /// <returns>返回文件内容</returns>        public static string ReadFile(string path, System.Text.Encoding encoding)        {            string result;            if (!System.IO.File.Exists(path))            {                result = "不存在相应的目录";            }            else            {                System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);                System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding);                result = streamReader.ReadToEnd();                streamReader.Close();                streamReader.Dispose();            }            return result;        }

 

向指定文件写入内容

  

      /// <summary>        /// 向指定文件写入内容        /// </summary>        /// <param name="path">要写入内容的文件完整路径</param>        /// <param name="content">要写入的内容</param>  public static void WriteFile(string path, string content)  {   try   {    object obj = new object();    if (!System.IO.File.Exists(path))    {     System.IO.FileStream fileStream = System.IO.File.Create(path);     fileStream.Close();    }    lock (obj)    {     using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default))     {      streamWriter.WriteLine(content);      streamWriter.Close();      streamWriter.Dispose();     }    }   }   catch (System.Exception ex)   {                ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}异常:{1}", path, ex.Message), ex);   }  }

 

指定编码格式向文件写入内容

 

       /// <summary>        /// 向指定文件写入内容        /// </summary>        /// <param name="path">要写入内容的文件完整路径</param>        /// <param name="content">要写入的内容</param>        /// <param name="encoding">编码格式</param>        public static void WriteFile(string path, string content, System.Text.Encoding encoding)        {            try            {                object obj = new object();                if (!System.IO.File.Exists(path))                {                    System.IO.FileStream fileStream = System.IO.File.Create(path);                    fileStream.Close();                }                lock (obj)                {                    using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding))                    {                        streamWriter.WriteLine(content);                        streamWriter.Close();                        streamWriter.Dispose();                    }                }            }            catch (System.Exception ex)            {                ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}异常:{1}", path, ex.Message), ex);            }        }

 

 

文件复制

        /// <summary>        /// 文件复制        /// </summary>        /// <param name="orignFile">源文件完整路径</param>        /// <param name="newFile">目标文件完整路径</param>  public static void FileCoppy(string orignFile, string newFile)  {   System.IO.File.Copy(orignFile, newFile, true);  }

 

文件删除

 

       /// <summary>        /// 删除文件        /// </summary>        /// <param name="path">要删除的文件的完整路径</param>  public static void FileDel(string path)  {   System.IO.File.Delete(path);  }

 

文件移动

 

       /// <summary>        /// 文件移动(剪贴->粘贴)        /// </summary>        /// <param name="orignFile">源文件的完整路径</param>        /// <param name="newFile">目标文件完整路径</param>  public static void FileMove(string orignFile, string newFile)  {   System.IO.File.Move(orignFile, newFile);  }

 

判断一组文件是否都存在

   

     /// <summary>        /// 判断一组文件是否都存在        /// </summary>        /// <param name="filePathList">文件路径List</param>        /// <returns>文件是否全部存在</returns>        public static bool IsFilesExist(List<string> filePathList)        {            bool isAllExist = true;            foreach(string filePath in filePathList)            {                if(!File.Exists(filePath))                {                    isAllExist = false;                }            }            return isAllExist;        }

 

创建目录

 

       /// <summary>        /// 创建目录        /// </summary>        /// <param name="orignFolder">当前目录</param>        /// <param name="newFloder">要创建的目录名</param>  public static void FolderCreate(string orignFolder, string newFloder)  {   System.IO.Directory.SetCurrentDirectory(orignFolder);   System.IO.Directory.CreateDirectory(newFloder);  }

 

删除目录

   

     /// <summary>        /// 删除目录        /// </summary>        /// <param name="dir">要删除的目录</param>  public static void DeleteFolder(string dir)  {   if (System.IO.Directory.Exists(dir))   {    string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);    for (int i = 0; i < fileSystemEntries.Length; i++)    {     string text = fileSystemEntries[i];     if (System.IO.File.Exists(text))     {      System.IO.File.Delete(text);     }     else     {      FileHelper.DeleteFolder(text);     }    }    System.IO.Directory.Delete(dir);   }  }

 

目录内容复制

   

     /// <summary>        /// 目录内容复制        /// </summary>        /// <param name="srcPath">源目录</param>        /// <param name="aimPath">目标目录</param>  public static void CopyDir(string srcPath, string aimPath)  {   try   {    if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)    {     aimPath += System.IO.Path.DirectorySeparatorChar;    }    if (!System.IO.Directory.Exists(aimPath))    {     System.IO.Directory.CreateDirectory(aimPath);    }    string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(srcPath);    string[] array = fileSystemEntries;    for (int i = 0; i < array.Length; i++)    {     string text = array[i];     if (System.IO.Directory.Exists(text))     {      FileHelper.CopyDir(text, aimPath + System.IO.Path.GetFileName(text));     }     else     {      System.IO.File.Copy(text, aimPath + System.IO.Path.GetFileName(text), true);     }    }   }   catch (System.Exception ex)   {    throw new System.Exception(ex.ToString());   }  }

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ASPX文件转HTML
NET问答: 有最干净利落的读写文件方式吗?
Hi! 欢迎来到 [c#,文件读写的操作
网页静态化,生成静态文件的类
Lock不住的BUG,神一般无解的BUG(XX正由另一进程使用,因此该进程无法访问该文件)
浅析C#中的文件操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服