• 文本文件从磁盘读取、写入


    本文转自CSDN博客:http://blog.csdn.net/anchenyanyue/article/details/7666370

      1 using System;
      2 using System.Text;
      3 using System.IO;
      4 
      5 namespace XXXX.Common
      6 {
      7     /// <summary>
      8     /// 文本文件从磁盘读取、写入
      9     /// </summary>
     10     public class FileHelper
     11     {       
     12 
     13         /// <summary>
     14         /// 从文件中读取文本内容
     15         /// </summary>
     16         /// <param name="filePath">文本路径</param>
     17         /// <returns>返回文件中的内容</returns>
     18         public static string Read(string filePath)
     19         {
     20             string result = string.Empty;
     21             if (File.Exists(filePath))
     22             {
     23                 using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
     24                 {
     25                     result = sr.ReadToEnd();
     26                     sr.Close();
     27                     return result;
     28                 }
     29             }
     30             return result;
     31         }
     32 
     33         /// <summary>
     34         /// 写入文本文件,按默认编码
     35         /// </summary>
     36         /// <param name="filePath">文本文件路径包括文件名</param>
     37         /// <param name="content">写入内容</param>
     38         public static void Write(string filePath, string content)
     39         {
     40             using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default))
     41             {
     42                 sw.Write(content);
     43                 sw.Close();
     44             }
     45         }
     46         /// <summary>
     47         /// 写入文本文件,以UFT8格式编码
     48         /// </summary>
     49         /// <param name="filePath">文本文件路径包括文件名</param>
     50         /// <param name="content">写入内容</param>
     51         /// <param name="UTF8"></param>
     52         public static void Write(string filePath, string content, bool UTF8)
     53         {
     54             using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
     55             {
     56                 sw.Write(content);
     57                 sw.Close();
     58             }
     59         }
     60 
     61         /// <summary>
     62         /// 备份文件
     63         /// </summary>
     64         /// <param name="sourceFileName">源文件路径</param>
     65         /// <param name="destFileName">目标文件路径</param>
     66         /// <param name="overwrite">是否覆盖已存在文件</param>
     67         /// <returns></returns>
     68         public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)
     69         {
     70             bool flag;
     71             if (!File.Exists(sourceFileName))
     72             {
     73                 throw new FileNotFoundException(sourceFileName + "文件不存在!");
     74             }
     75             if (!overwrite && File.Exists(destFileName))
     76             {
     77                 return false;
     78             }
     79             try
     80             {
     81                 File.Copy(sourceFileName, destFileName, true);
     82                 flag = true;
     83             }
     84             catch (Exception exception)
     85             {
     86                 throw exception;
     87             }
     88             return flag;
     89         }
     90 
     91         /// <summary>
     92         /// 拷贝文件夹文件
     93         /// </summary>
     94         /// <param name="srcDir"></param>
     95         /// <param name="dstDir"></param>
     96         public static void CopyDirFiles(string srcDir, string dstDir)
     97         {
     98             if (Directory.Exists(srcDir))
     99             {
    100                 foreach (string str in Directory.GetFiles(srcDir))
    101                 {
    102                     string destFileName = Path.Combine(dstDir, Path.GetFileName(str));
    103                     File.Copy(str, destFileName, true);
    104                 }
    105 
    106                 foreach (string str3 in Directory.GetDirectories(srcDir))
    107                 {
    108                     string str4 = str3.Substring(str3.LastIndexOf(@"") + 1);
    109                     CopyDirFiles(Path.Combine(srcDir, str4), Path.Combine(dstDir, str4));
    110                 }
    111             }
    112         }
    113 
    114         /// <summary>
    115         /// 删除文件夹文件
    116         /// </summary>
    117         /// <param name="dir"></param>
    118         public static void DeleteDirFiles(string dir)
    119         {
    120             if (Directory.Exists(dir))
    121             {
    122                 foreach (string str in Directory.GetFiles(dir))
    123                 {
    124                     File.Delete(str);
    125                 }
    126                 foreach (string str2 in Directory.GetDirectories(dir))
    127                 {
    128                     Directory.Delete(str2, true);
    129                 }
    130             }
    131         }
    132 
    133         /// <summary>
    134         /// 获得文件最后修改时间
    135         /// </summary>
    136         /// <param name="file"></param>
    137         /// <returns></returns>
    138         public static DateTime GetFileLastWriteTime(string file)
    139         {
    140             if (File.Exists(file))
    141             {
    142                 return File.GetLastWriteTime(file);
    143             }
    144             return DateTime.MaxValue;
    145         }
    146 
    147         /// <summary>
    148         /// 取消文件的只读属性
    149         /// </summary>
    150         /// <param name="file"></param>
    151         public static void RemoveFileReadOnlyAttribute(string file)
    152         {
    153             File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);
    154         }         
    155     }
    156 }
  • 相关阅读:
    19. vue的原理
    18.jwt加密
    17.vue移动端项目二
    16.vue-cli跨域,swiper,移动端项目
    15.vue动画& vuex
    14.vue路由&脚手架
    13.vue组件
    12.vue属性.监听.组件
    11.vue 数据交互
    从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/KTblog/p/4209546.html
Copyright © 2020-2023  润新知