一. IO读写  

  这里主要包括文件的读、写、移动、复制、删除、文件夹的创建、文件夹的删除等常规操作。

注意:这里需要特别注意,对于普通的控制台程序和Web程序,将"相对路径"转换成"绝对路径"的方法不一致。

(1). 在web程序中,可以使用HttpContext.Current.Server.MapPath进行转换,使用方法通过 ~/定位到一级目录,eg:FileOperateHelp.Write_Txt("~/TestFile/mr.txt", "mr")。

(2). 在普通的控制台程序中,HttpContext.Current为空,所以web中的转换方式在控制台中失效,需要借助 "AppDomain.CurrentDomain.BaseDirectory"来获取根目录,该目录直接定位到Debug文件夹,然后利用System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath)传入路径与根目录进行结合,所以普通的控制台程序生成的所有文件均在Debug文件夹下,当然,你也可以直接写绝对路径,比如D:xxx。

   下面的方法就是专门处理普通程序和web程序路径转换问题的。

 1         /// <summary>
 2         /// 10.将相对路径转换成绝对路径
 3         /// </summary>
 4         /// <param name="strPath">相对路径</param>
 5         public static string PathConvert(string strPath)
 6         {
 7             //web程序使用
 8             if (HttpContext.Current != null)
 9             {
10                 return HttpContext.Current.Server.MapPath(strPath);
11             }
12             else //非web程序引用             
13             {
14                 strPath = strPath.Replace("/", "\\");
15                 if (strPath.StartsWith("\\"))
16                 {
17                     strPath = strPath.TrimStart('\\');
18                 }
19                 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
20             }
21         }

 

下面分享一个FileOperateHelp类,里面包含IO的常规操作。

  1  /// <summary>
  2     /// 文件操作类
  3     /// 特别注意:
  4     /// 1.在非web程序中,HttpContext.Current.Server.MapPath失效不好用,需要使用System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
  5     ///   获取本目录,然后写到根目录里。AppDomain.CurrentDomain.BaseDirectory将获取到Debug文件夹,无法使用相对路径
  6     /// 2.在web程序里,可以使用HttpContext.Current.Server.MapPath进行转换,使用方法通过 ~/定位到一级目录,eg:FileOperateHelp.Write_Txt("~/TestFile/mr.txt", "mr");
  7     /// </summary>
  8     public static class FileOperateHelp
  9     {
 10 
 11         #region 01.写文件(.txt-覆盖)
 12         /// <summary>
 13         /// 写文件(覆盖源文件内容)
 14         /// 文件不存在的话自动创建
 15         /// </summary>
 16         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>
 17         /// <param name="Content">文件内容</param>
 18         public static string Write_Txt(string FileName, string Content)
 19         {
 20             try
 21             {
 22                 Encoding code = Encoding.GetEncoding("gb2312");
 23                 string htmlfilename = FileOperateHelp.PathConvert(FileName);
 24                 //string htmlfilename = HttpContext.Current.Server.MapPath(FileName + ".txt"); //保存文件的路径  
 25                 string str = Content;
 26                 StreamWriter sw = null;
 27                 {
 28                     try
 29                     {
 30                         sw = new StreamWriter(htmlfilename, false, code);
 31                         sw.Write(str);
 32                         sw.Flush();
 33                     }
 34                     catch { }
 35                 }
 36                 sw.Close();
 37                 sw.Dispose();
 38                 return "ok";
 39             }
 40             catch (Exception ex)
 41             {
 42 
 43                 return ex.Message;
 44             }
 45 
 46         }
 47         #endregion
 48 
 49         #region 02.读文件(.txt)
 50         /// <summary>
 51         /// 读文件
 52         /// </summary>
 53         /// <param name="filename">文件路径(web里相对路径,控制台在根目录下写)</param>
 54         /// <returns></returns>
 55         public static string Read_Txt(string filename)
 56         {
 57 
 58             try
 59             {
 60                 Encoding code = Encoding.GetEncoding("gb2312");
 61                 string temp = FileOperateHelp.PathConvert(filename);
 62                 //  string temp = HttpContext.Current.Server.MapPath(filename + ".txt");
 63                 string str = "";
 64                 if (File.Exists(temp))
 65                 {
 66                     StreamReader sr = null;
 67                     try
 68                     {
 69                         sr = new StreamReader(temp, code);
 70                         str = sr.ReadToEnd(); // 读取文件  
 71                     }
 72                     catch { }
 73                     sr.Close();
 74                     sr.Dispose();
 75                 }
 76                 else
 77                 {
 78                     str = "";
 79                 }
 80                 return str;
 81             }
 82             catch (Exception ex)
 83             {
 84                 
 85                   return ex.Message;
 86             }
 87         }
 88         #endregion
 89 
 90         #region 03.写文件(.txt-添加)
 91         /// <summary>  
 92         /// 写文件  
 93         /// </summary>  
 94         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>  
 95         /// <param name="Strings">文件内容</param>  
 96         public static string WriteFile(string FileName, string Strings)
 97         {
 98             try
 99             {
100                 string Path = FileOperateHelp.PathConvert(FileName);
101 
102                 if (!System.IO.File.Exists(Path))
103                 {
104                     System.IO.FileStream f = System.IO.File.Create(Path);
105                     f.Close();
106                     f.Dispose();
107                 }
108                 System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
109                 f2.WriteLine(Strings);
110                 f2.Close();
111                 f2.Dispose();
112                 return "ok";
113             }
114             catch (Exception ex)
115             {
116 
117                 return ex.Message;
118             }
119         }
120         #endregion
121 
122         #region 04.读文件(.txt)
123         /// <summary>  
124         /// 读文件  
125         /// </summary>  
126         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>  
127         /// <returns></returns>  
128         public static string ReadFile(string FileName)
129         {
130             try
131             {
132                 string Path = FileOperateHelp.PathConvert(FileName);
133                 string s = "";
134                 if (!System.IO.File.Exists(Path))
135                     s = "不存在相应的目录";
136                 else
137                 {
138                     StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
139                     s = f2.ReadToEnd();
140                     f2.Close();
141                     f2.Dispose();
142                 }
143                 return s;
144             }
145             catch (Exception ex)
146             {
147                 return ex.Message;
148             }
149         }
150         #endregion
151 
152         #region 05.删除文件
153         /// <summary>  
154         /// 删除文件  
155         /// </summary>  
156         /// <param name="Path">文件路径(web里相对路径,控制台在根目录下写)</param>  
157         public static string FileDel(string Path)
158         {
159             try
160             {
161                 string temp = FileOperateHelp.PathConvert(Path);
162                 File.Delete(temp);
163                 return "ok";
164             }
165             catch (Exception ex)
166             {
167                 return ex.Message;
168             }
169         }
170         #endregion
171 
172         #region 06.移动文件
173         /// <summary>  
174         /// 移动文件  
175         /// </summary>  
176         /// <param name="OrignFile">原始路径(web里相对路径,控制台在根目录下写)</param>  
177         /// <param name="NewFile">新路径,需要写上路径下的文件名,不能单写路径(web里相对路径,控制台在根目录下写)</param>  
178         public static string FileMove(string OrignFile, string NewFile)
179         {
180             try
181             {
182                 OrignFile = FileOperateHelp.PathConvert(OrignFile);
183                 NewFile = FileOperateHelp.PathConvert(NewFile);
184                 File.Move(OrignFile, NewFile);
185                 return "ok";
186             }
187             catch (Exception ex)
188             {
189                 return ex.Message;
190             }
191         }
192         #endregion
193 
194         #region 07.复制文件
195         /// <summary>  
196         /// 复制文件  
197         /// </summary>  
198         /// <param name="OrignFile">原始文件(web里相对路径,控制台在根目录下写)</param>  
199         /// <param name="NewFile">新文件路径(web里相对路径,控制台在根目录下写)</param>  
200         public static string FileCopy(string OrignFile, string NewFile)
201         {
202             try
203             {
204                 OrignFile = FileOperateHelp.PathConvert(OrignFile);
205                 NewFile = FileOperateHelp.PathConvert(NewFile);
206                 File.Copy(OrignFile, NewFile, true);
207                 return "ok";
208             }
209             catch (Exception ex)
210             {
211                 return ex.Message;
212             }
213         }
214         #endregion
215         
216         #region 08.创建文件夹
217         /// <summary>  
218         /// 创建文件夹  
219         /// </summary>  
220         /// <param name="Path">相对路径(web里相对路径,控制台在根目录下写)</param>  
221         public static string FolderCreate(string Path)
222         {
223             try
224             {
225                 Path = FileOperateHelp.PathConvert(Path);
226                 // 判断目标目录是否存在如果不存在则新建之  
227                 if (!Directory.Exists(Path))
228                 {
229                     Directory.CreateDirectory(Path);
230                 }
231                 return "ok";
232             }
233             catch (Exception ex)
234             {
235                 return ex.Message;
236             }
237         }
238         #endregion
239 
240         #region 09.递归删除文件夹目录及文件
241         /// <summary>  
242         /// 递归删除文件夹目录及文件  
243         /// </summary>  
244         /// <param name="dir">相对路径(web里相对路径,控制台在根目录下写) 截止到哪删除到哪,eg:/a/ 连a也删除</param>    
245         /// <returns></returns>  
246         public static string DeleteFolder(string dir)
247         {
248 
249             try
250             {
251                 string adir = FileOperateHelp.PathConvert(dir);
252                 if (Directory.Exists(adir)) //如果存在这个文件夹删除之   
253                 {
254                     foreach (string d in Directory.GetFileSystemEntries(adir))
255                     {
256                         if (File.Exists(d))
257                             File.Delete(d); //直接删除其中的文件                          
258                         else
259                             DeleteFolder(d); //递归删除子文件夹   
260                     }
261                     Directory.Delete(adir, true); //删除已空文件夹                   
262                 }
263                 return "ok";
264             }
265             catch (Exception ex)
266             {
267                 return ex.Message;
268             }
269         }
270 
271         #endregion
272 
273         #region 10.将相对路径转换成绝对路径
274         /// <summary>
275         /// 10.将相对路径转换成绝对路径
276         /// </summary>
277         /// <param name="strPath">相对路径</param>
278         public static string PathConvert(string strPath)
279         {
280             //web程序使用
281             if (HttpContext.Current != null)
282             {
283                 return HttpContext.Current.Server.MapPath(strPath);
284             }
285             else //非web程序引用             
286             {
287                 strPath = strPath.Replace("/", "\\");
288                 if (strPath.StartsWith("\\"))
289                 {
290                     strPath = strPath.TrimStart('\\');
291                 }
292                 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
293             }
294         }
295         #endregion
296 
297 
298 
299     }
View Code

相关文章:

  • 2021-11-28
  • 2022-12-23
  • 2022-01-13
  • 2021-11-17
  • 2021-08-23
  • 2022-03-01
  • 2021-04-22
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-02-20
  • 2022-01-24
相关资源
相似解决方案