场景

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

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

  1. java 文件拷贝保留原来的属性_Java常用属性拷贝工具类使用总结

    开头聊几句 1.网上很多的技术文章和资料是有问题的,要学会辨证的看待,不能随便就拿来用,起码要自己验证一下 2.关注当下,关注此刻,如果你真正阅读本篇文章,请花几分钟时间的注意力阅读,相信你会有收获的 ...

  2. java中常用的日期工具类

    java中常用的日期工具类 日期相关的类: package net.yto.ofclacct.core.util;import java.text.ParseException; import jav ...

  3. 常用Apache Commons工具类备忘

    常用Apache Commons工具类 ----------------------------------------------------------------- 例如:commons.lan ...

  4. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  5. java工具类怎么写_常用的Java工具类——十六种

    常用的Java工具类--十六种 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选 ...

  6. java中使用jxl导出excel表格的工具类(全网唯一亲测可用,在原来基础上扩展)

    java中后台导出excel的话,有两种方案,一是使用poi(不过由于是windows版本的,存在不兼容,但功能更多,更强大),而是使用jxl(纯java编写,不过兼容,简单一些),可以设置输出的ex ...

  7. Java中使用Base64进行编码解码的工具类-将验证码图片使用Base64编码并返回给前端

    场景 前端使用Vue,验证码图片的src属性来自于后台SpringBoot接口. 后台验证码接口生成验证码图片并将其使用Base64进行编码. 前端就可以直接使用 data:image/png;bas ...

  8. go语言中的文件file操作

    一.File文件操作 首先,file类是在os包中的,封装了底层的文件描述符和相关信息,同时封装了Read和Write的实现. 1.FileInfo接口 FileInfo接口中定义了File信息相关的 ...

  9. java中常用到的工具类使用

    Tool 不定期更新,建议收藏,收录日常所用 1,判断对象是否为空的常用工具类 2,对象和数组的复制 3,关于拼接字符串去掉最后一个符号的三种方式 4,判断对象值属性不为null并且不为空字符串 5, ...

最新文章

  1. R语言编程艺术(1)快速入门
  2. mysql 优化语句
  3. 您没有足够的全新为该计算机所有用户安装,很抱歉,无法安装Office(64位),因为您的计算机上已经安装了这些32位Office程序解决办法...
  4. 基于opencv和mfc的摄像头采集代码(GOMFCTemplate2)持续更新
  5. DML、DDl、DQL实战
  6. 雅虎的Mash-up 之路
  7. Entity Framework 6新特性:全局性地自定义Code First约定
  8. pythonnamedtuple定义类型_python - namedtuple和可选关键字参数的默认值
  9. 【模型压缩】Only Train Once:微软、浙大等研究者提出剪枝框架OTO,无需微调即可获得轻量级架构...
  10. 国外的程序员都是什么样的状态?硅谷程序员:不加班,不穿女装
  11. Mysql删除数据报外键约束解决方法
  12. php 豆瓣api_豆瓣网api使用方式
  13. FLASH和EEPROM的最大区别
  14. C++ vector 初始化大小
  15. IT软件资产管理流程梳理介绍
  16. 北京交通大学计算机仿真大作业直流调速系统仿真,计算机仿真技术大作业 12脉波整流电路仿真.doc...
  17. FL Studio的音频录制插件Edison
  18. 冒险岛 mysql 添加账号密码_Win7系统玩冒险岛079单机版输入账号密码后出现error38怎么办...
  19. UVM搭建 ------ 进阶DIY教程
  20. ftp服务器文件传输,FTP服务器之间传输文件

热门文章

  1. 企业微信oauth认证_OAuth2身份认证
  2. HTTP状态码:204 No Content(总结HTTP状态码)
  3. Java的自动装箱与自动拆箱
  4. python语言的解释性特点指的是编写的程序不需要编译_解释性与编译型 Python2和python3的区别...
  5. ue4 classuobject没有成员beginplay_UE4中蓝图函数的泛型
  6. python 福利彩票_使用Python买福彩,5个数字,20选5,有没买过
  7. python画五角星填充不同颜色_Python绘制分形树(一)
  8. java gc信息_JVM之GC回收信息详解
  9. c语言 链表 库,玩转C链表
  10. java 压缩技术_Java压缩技术(三) ZIP解压缩——Java原生实现