欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-08 20:53:28
场景 C#中File类的常用读取与写入文件方法的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983 注: 博客主页:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸 ......

场景

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

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

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

获取文件的扩展名

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