博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通用类 FileHelper 文本文件从磁盘读取、写入
阅读量:7052 次
发布时间:2019-06-28

本文共 3901 字,大约阅读时间需要 13 分钟。

///     /// 文本文件从磁盘读取、写入    ///     public class FileHelper    {               ///         /// 从文件中读取文本内容        ///         /// 文本路径        /// 
返回文件中的内容
public static string Read(string filePath) { string result = string.Empty; if (File.Exists(filePath)) { using (StreamReader sr = new StreamReader(filePath, Encoding.Default)) { result = sr.ReadToEnd(); sr.Close(); return result; } } return result; } /// /// 写入文本文件,按默认编码 /// /// 文本文件路径包括文件名 /// 写入内容 public static void Write(string filePath, string content) { using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default)) { sw.Write(content); sw.Close(); } } /// /// 写入文本文件,以UFT8格式编码 /// /// 文本文件路径包括文件名 /// 写入内容 /// public static void Write(string filePath, string content, bool UTF8) { using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8)) { sw.Write(content); sw.Close(); } } /// /// 备份文件 /// /// 源文件路径 /// 目标文件路径 /// 是否覆盖已存在文件 ///
public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite) { bool flag; if (!File.Exists(sourceFileName)) { throw new FileNotFoundException(sourceFileName + "文件不存在!"); } if (!overwrite && File.Exists(destFileName)) { return false; } try { File.Copy(sourceFileName, destFileName, true); flag = true; } catch (Exception exception) { throw exception; } return flag; } /// /// 拷贝文件夹文件 /// /// /// public static void CopyDirFiles(string srcDir, string dstDir) { if (Directory.Exists(srcDir)) { foreach (string str in Directory.GetFiles(srcDir)) { string destFileName = Path.Combine(dstDir, Path.GetFileName(str)); File.Copy(str, destFileName, true); } foreach (string str3 in Directory.GetDirectories(srcDir)) { string str4 = str3.Substring(str3.LastIndexOf(@"\") + 1); CopyDirFiles(Path.Combine(srcDir, str4), Path.Combine(dstDir, str4)); } } } /// /// 删除文件夹文件 /// /// public static void DeleteDirFiles(string dir) { if (Directory.Exists(dir)) { foreach (string str in Directory.GetFiles(dir)) { File.Delete(str); } foreach (string str2 in Directory.GetDirectories(dir)) { Directory.Delete(str2, true); } } } /// /// 获得文件最后修改时间 /// /// ///
public static DateTime GetFileLastWriteTime(string file) { if (File.Exists(file)) { return File.GetLastWriteTime(file); } return DateTime.MaxValue; } /// /// 取消文件的只读属性 /// /// public static void RemoveFileReadOnlyAttribute(string file) { File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly); } }

 

转载于:https://www.cnblogs.com/acyy/archive/2012/08/29/2662219.html

你可能感兴趣的文章
xshell使用技巧(工欲善其事,必先利其器)
查看>>
Connections could not be acquired from the underl
查看>>
history详解
查看>>
使用TAR源码包安装程序
查看>>
MSF目录结构
查看>>
RHEL下部署heartbeat,实现简单故障转移群集
查看>>
SQL如何分批次查询
查看>>
Swift可选值Optionals
查看>>
VMware Workstation Pro 調整硬盤空間(下)
查看>>
在线将Apache Rewrite伪静态规则自动转换为Nginx Rewrite
查看>>
Hibernate实现,使用UUID.主键的生成策略
查看>>
在工作中经常使用的git命令笔记
查看>>
Centos6.4安装mysql-5.5.33绿色版
查看>>
Java反射
查看>>
vmware安装 深度完美ghost winXP SP3 详细图文教程,强调一些重难点与技巧,模拟生产环境必备!...
查看>>
为什么我的日志文件不能继续记录呢
查看>>
如何安装CRX格式?Chrome插件离线安装,CRX格式安装方法 JSON-handle Chrome插件下载...
查看>>
使用Azure Function + Cognitive Services 实现图片自动化审核
查看>>
两种高性能 I/O 设计模式 Reactor 和 Proactor
查看>>
Redis进阶实践之九 独立封装的RedisClient客户端工具类
查看>>