• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬伊天?
博客园    首页    新随笔    联系   管理    订阅  订阅

002-FileIO(文件读写)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Text;
  5 using System.Windows.Forms;
  6 
  7 namespace SuperDLL
  8 {
  9     /// <summary>
 10     /// 文件IO
 11     /// </summary>
 12     public static class FileIO
 13     {
 14         /// <summary>
 15         /// 程序所在文件夹
 16         /// </summary>
 17         public static string currentPath = System.Environment.CurrentDirectory + "\\";
 18         /// <summary>
 19         /// 创建路径
 20         /// </summary>
 21         /// <param name="pathStr"></param>
 22         public static void CreatePath(string pathStr)
 23         {
 24             if (!Directory.Exists(pathStr))
 25             {
 26                 Directory.CreateDirectory(pathStr);
 27             }
 28         }
 29         /// <summary>
 30         /// 浏览目录
 31         /// </summary>
 32         /// <returns></returns>
 33         public static string BrowsePath()
 34         {
 35             using (OpenFileDialog dialog = new OpenFileDialog())
 36             {
 37                 dialog.Multiselect = false;
 38                 dialog.Title = "请选择文件";
 39                 dialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
 40                 string Path = null;
 41                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 42                 {
 43                     Path = dialog.FileName;
 44                 }
 45                 return Path;
 46             }
 47         }
 48         /// <summary>
 49         /// 选择保存目录
 50         /// </summary>
 51         private void BrowseClick()
 52         {
 53             FolderBrowserDialog dialog = new FolderBrowserDialog();
 54             dialog.Description = "请选择保存目录";
 55             if (dialog.ShowDialog() == DialogResult.OK)
 56             {
 57                 string path = dialog.SelectedPath;
 58             }
 59         }
 60         /// <summary>
 61         /// 读文件
 62         /// </summary>
 63         /// <param name="pathStr"></param>
 64         /// <param name="nameStr"></param>
 65         /// <returns></returns>
 66         public static string ReadContent(string pathStr, string nameStr)
 67         {
 68             string contextStr = null;
 69             if (System.IO.File.Exists(pathStr + nameStr))
 70             {
 71                 using (FileStream fsSource = new FileStream(pathStr + nameStr, FileMode.Open, FileAccess.Read))
 72                 {
 73                     byte[] bytes = new byte[fsSource.Length];
 74                     int numBytesToRead = (int)fsSource.Length;
 75                     int numBytesRead = 0;
 76                     while (numBytesToRead > 0)
 77                     {
 78                         int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
 79                         if (n == 0)
 80                         {
 81                             break;
 82                         }
 83                         else
 84                         {
 85                             numBytesRead += n;
 86                             numBytesToRead -= n;
 87                         }
 88                     }
 89                     contextStr = Encoding.UTF8.GetString(bytes);
 90                 }
 91             }
 92             return contextStr;
 93         }
 94         /// <summary>
 95         /// 写文件
 96         /// </summary>
 97         /// <param name="pathStr"></param>
 98         /// <param name="nameStr"></param>
 99         /// <param name="contextStr"></param>
100         public static void WriteContent(string pathStr, string nameStr, string contextStr)
101         {
102             CreatePath(pathStr);
103             using (FileStream fsWrite = new FileStream(pathStr + nameStr, FileMode.Create))
104             {
105                 byte[] data = new UTF8Encoding().GetBytes(contextStr);
106                 fsWrite.Write(data, 0, data.Length);
107                 fsWrite.Flush();
108             }
109         }
110         /// <summary>
111         /// 写日志
112         /// </summary>
113         /// <param name="pathStr"></param>
114         /// <param name="nameStr"></param>
115         /// <param name="contextStr"></param>
116         public static void IncreaseContent(string pathStr, string nameStr, string contextStr)
117         {
118             CreatePath(pathStr);
119             if (System.IO.File.Exists(pathStr + nameStr))
120             {
121                 using (FileStream fsIncrease = File.OpenWrite(pathStr + nameStr))
122                 {
123                     Encoding encoder = Encoding.UTF8;
124                     byte[] bytes = encoder.GetBytes(contextStr);
125                     fsIncrease.Position = fsIncrease.Length;
126                     fsIncrease.Write(bytes, 0, bytes.Length);
127                     fsIncrease.Flush();
128                 }
129             }
130             else
131             {
132                 using (FileStream fsCreate = new FileStream(pathStr + nameStr, FileMode.Create, FileAccess.Write))
133                 {
134                     Encoding encoder = Encoding.UTF8;
135                     byte[] bytes = encoder.GetBytes(contextStr);
136                     fsCreate.Position = fsCreate.Length;
137                     fsCreate.Write(bytes, 0, bytes.Length);
138                     fsCreate.Flush();
139                 }
140             }
141         }
142     }
143 }

 

posted @ 2018-09-08 19:18  灬伊天☂  阅读(198)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3