文件操作类 实现文件的基本操作,读取、写入、删除、修改和文件夹操作
文件操作类 实现文件的基本操作,读取、写入、删除、修改和文件夹操作
View Code
1 public class FileIO
2 {
3 #region 写文件 根据形式
4 /// <summary>
5 /// 写文件
6 /// </summary>
7 /// <param name="FilePath"> </param>
8 /// <param name="Append">是否为追加 </param>
9 /// <param name="FileString"> </param>
10 public void WriterFile(string FilePath, bool Append, string FileString)
11 {
12 using (StreamWriter objWriter = new StreamWriter(FilePath, Append, Encoding.UTF8))
13 {
14 objWriter.Write(FileString);
15 objWriter.Flush();
16 objWriter.Close();
17 }
18 }
19 #endregion
20
21 #region 写文件
22 /// <summary>
23 /// 写文件
24 /// </summary>
25 /// <param name="FilePath"> </param>
26 /// <param name="FileString"> </param>
27 public void WriterFile(string FilePath, string FileString)
28 {
29 WriterFile(FilePath, FileString, Encoding.UTF8);
30 }
31 #endregion
32
33 #region 写文件 路径和内容
34 /// <summary>
35 /// 写文件
36 /// </summary>
37 /// <param name="FilePath">文件路径</param>
38 /// <param name="FileString">文件内容</param>
39 public void WriterFile(string FilePath, string FileString, Encoding encoding)
40 {
41 string path = Path.GetDirectoryName(FilePath);
42 if (Directory.Exists(path) == false)
43 {
44 Directory.CreateDirectory(path);
45 }
46
47
48 using (StreamWriter objWriter = new StreamWriter(FilePath, false, encoding))
49 {
50 objWriter.Write(FileString);
51 objWriter.Flush();
52 objWriter.Close();
53 }
54
55
56 }
57 #endregion
58
59 #region 读取文件
60 /// <summary>
61 /// 读文件
62 /// </summary>
63 /// <param name="filePath">文件路径</param>
64 public static string ReadFile(string filePath)
65 {
66 return ReadFile(filePath, Encoding.Default);
67 }
68 #endregion
69
70 #region 读取文件 自定义读取方式
71 /// <summary>
72 /// 读文件
73 /// </summary>
74 /// <param name="filePath">文件路径</param>
75 /// <param name="encoding">编码方式</param>
76 public static string ReadFile(string filePath, Encoding encoding)
77 {
78 string outPut = string.Empty;
79 if (File.Exists(filePath))
80 {
81 try
82 {
83 StreamReader fileStream = new StreamReader(filePath, System.Text.Encoding.Default);
84 outPut = fileStream.ReadToEnd();
85 fileStream.Close();
86 fileStream.Dispose();
87 }
88 catch (IOException e)
89 {
90 throw new IOException(e.ToString());
91 }
92 }
93 else
94 {
95 outPut = "找不到相应的文件!";
96 }
97 return outPut;
98 }
99 #endregion
100
101 #region 保存文件
102 /// <summary>
103 /// 保存文件
104 /// </summary>
105 /// <param name="path">路径</param>
106 /// <param name="fileContent">文件内容</param>
107 /// <returns></returns>
108 public static bool SaveFile(string path, string fileContent)
109 {
110 return SaveFile(path, fileContent, Encoding.Default);
111 }
112 #endregion
113
114 #region 保存文件 自定义保存方式
115 /// <summary>
116 /// 保存文件
117 /// </summary>
118 /// <param name="path">路径</param>
119 /// <param name="fileContent">文件内容</param>
120 /// <param name="encoding">编码方式</param>
121 /// <returns></returns>
122 public static bool SaveFile(string path, string fileContent, Encoding encoding)
123 {
124 bool result = false;
125
126 //判断目录是否存在
127 string dirPath = path.Substring(0, path.LastIndexOf("\\"));
128 DirectoryInfo dir = new DirectoryInfo(dirPath);
129 if (!dir.Exists)
130 {
131 dir.Create();
132 }
133
134 try
135 {
136 StreamWriter Fso = new StreamWriter(path, false, encoding);
137 Fso.WriteLine(fileContent);
138 Fso.Close();
139 Fso.Dispose();
140
141 result = true;
142 }
143 catch (IOException e)
144 {
145 throw new IOException(e.ToString());
146 }
147 return result;
148 }
149 #endregion
150
151 #region 复制文件
152 /// <summary>
153 /// 复制文件
154 /// </summary>
155 /// <param name="src">源文件</param>
156 /// <param name="dest">目标文件</param>
157 /// <returns></returns>
158 public bool CopyFile(string src, string dest)
159 {
160 try
161 {
162 File.Copy(src , dest, true);
163 return true;
164 }
165 catch
166 {
167 return false;
168 }
169 }
170 #endregion
171
172 #region 复制目录及其文件往新目录
173 /// <summary>
174 /// 复制目录及其文件往新目录
175 /// </summary>
176 /// <param name="src">源目录 </param>
177 /// <param name="dest">目标目录 </param>
178 public void CopyDirectory(string src, string dest)
179 {
180 if (Directory.Exists(src) == false)
181 {
182 Directory.CreateDirectory(src);
183 }
184 if (Directory.Exists(dest) == true)
185 {
186 DeleteInDir(dest);
187 }
188 if (Directory.Exists(dest) == false)
189 {
190 Directory.CreateDirectory(dest);
191 }
192 DirectoryInfo di = new DirectoryInfo(src);
193 foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
194 {
195 string destName = Path.Combine(dest, fsi.Name);
196 if (fsi is FileInfo)
197 File.Copy(fsi.FullName, destName);
198 else
199 {
200 Directory.CreateDirectory(destName);
201 CopyDirectory(fsi.FullName, destName);
202 }
203 }
204 }
205 #endregion
206
207 #region 递归获取文件夹大小
208 /// <summary>
209 /// 递归获取文件夹大小
210 /// </summary>
211 /// <param name="path">文件夹物理路径 </param>
212 /// <returns> </returns>
213 public static long GetFilesSize(string path)
214 {
215 DirectoryInfo di = new DirectoryInfo(path);
216 long size = 0;
217 foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
218 {
219 if (fsi is FileInfo)
220 {
221 size += ((FileInfo)fsi).Length;
222 }
223 else
224 {
225 size += GetFilesSize(fsi.FullName);
226 }
227 }
228 return size;
229 }
230 #endregion
231
232 #region 转换文件大小的表达形式
233 /// <summary>
234 /// 转换文件大小的表达形式
235 /// </summary>
236 /// <param name="FileSize"> </param>
237 /// <returns> </returns>
238 public static string FileSize(double FileSize)
239 {
240 if ((FileSize < 1048576))//1048576=1024 * 1024
241 {
242
243 return string.Concat(Math.Round((FileSize / 1024), 1), " KB");
244 }
245 else
246 {
247
248 return string.Concat(Math.Round(FileSize / 1048576, 1), " MB");
249 }
250 }
251 #endregion
252
253 #region 文件流编码
254 /// <summary>
255 /// 文件流编码
256 /// </summary>
257 /// <param name="stream">文件流</param>
258 /// <param name="defaultEncoding">编辑方式</param>
259 /// <returns> </returns>
260 public Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
261 {
262 Encoding bigEndianUnicode = defaultEncoding;
263 if ((stream != null) && (stream.Length >= 2L))
264 {
265 byte num = 0;
266 byte num2 = 0;
267 byte num3 = 0;
268 long offset = stream.Seek(0L, SeekOrigin.Begin);
269 stream.Seek(0L, SeekOrigin.Begin);
270 num = Convert.ToByte(stream.ReadByte());
271 num2 = Convert.ToByte(stream.ReadByte());
272 if (stream.Length >= 3L)
273 {
274 num3 = Convert.ToByte(stream.ReadByte());
275 }
276 if (stream.Length >= 4L)
277 {
278 Convert.ToByte(stream.ReadByte());
279 }
280 if ((num == 0xfe) && (num2 == 0xff))
281 {
282 bigEndianUnicode = Encoding.BigEndianUnicode;
283 }
284 if (((num == 0xff) && (num2 == 0xfe)) && (num3 != 0xff))
285 {
286 bigEndianUnicode = Encoding.Unicode;
287 }
288 if (((num == 0xef) && (num2 == 0xbb)) && (num3 == 0xbf))
289 {
290 bigEndianUnicode = Encoding.UTF8;
291 }
292 stream.Seek(offset, SeekOrigin.Begin);
293 }
294 return bigEndianUnicode;
295 }
296 #endregion
297
298 #region 递归删除文件夹下的所有文件
299 /// <summary>
300 /// 递归删除文件夹下的所有文件
301 /// </summary>
302 /// <param name="szDirPath">文件夹物理路径</param>
303 public void DeleteInDir(string szDirPath)
304 {
305 if (szDirPath.Trim() == "" || !Directory.Exists(szDirPath))
306 return;
307 DirectoryInfo dirInfo = new DirectoryInfo(szDirPath);
308
309 FileInfo[] fileInfos = dirInfo.GetFiles();
310 if (fileInfos != null && fileInfos.Length > 0)
311 {
312 foreach (FileInfo fileInfo in fileInfos)
313 {
314 File.Delete(fileInfo.FullName); //删除文件
315 }
316 }
317
318 DirectoryInfo[] dirInfos = dirInfo.GetDirectories();
319 if (dirInfos != null && dirInfos.Length > 0)
320 {
321 foreach (DirectoryInfo childDirInfo in dirInfos)
322 {
323 this.DeleteInDir(szDirPath + "\\" +childDirInfo.ToString()); //递归
324 childDirInfo.Delete(true);
325 }
326 }
327 }
328 #endregion
329
330 #region 目录是否存在
331 /// <summary>
332 /// 目录是否存在
333 /// </summary>
334 /// <param name="szDirPath"></param>
335 /// <returns></returns>
336 public bool isExists(string szDirPath)
337 {
338 return Directory.Exists(szDirPath);
339 }
340 #endregion
341
342 #region 修改文件名(文件夹)名称
343 /// <summary>
344 /// 修改文件名(文件夹)名称
345 /// </summary>
346 /// <param name="path">路径</param>
347 /// <param name="oldname">原始名称</param>
348 /// <param name="newname">新名称</param>
349 /// <param name="type">0为文件夹,1为文件</param>
350 /// <returns>成功返回1</returns>
351 public static int EidtName(string path, string oldname, string newname, int type)
352 {
353 int result = 0;
354 if (type == 0)
355 {
356 if (Directory.Exists(path.TrimEnd('\\') + "\\" + oldname))
357 {
358 try
359 {
360 Directory.Move(path.TrimEnd('\\') + "\\" + oldname, path + "\\" + newname.Replace(".", ""));
361 }
362 catch (IOException e)
363 {
364 throw new Exception(e.ToString());
365 }
366 result = 1;
367 }
368 else
369 {
370 throw new Exception("参数传递错误!");
371 }
372 }
373 else
374 {
375 if (File.Exists(path.TrimEnd('\\') + "\\" + oldname))
376 {
377 try
378 {
379 File.Move(path.TrimEnd('\\') + "\\" + oldname, path + "\\" + newname);
380 }
381 catch (Exception e)
382 {
383 throw new Exception(e.ToString());
384 }
385 result = 1;
386 }
387 else
388 {
389 throw new Exception("参数传递错误!");
390 }
391 }
392 return result;
393 }
394 #endregion
395
396 #region 删除文件或文件夹
397 /// <summary>
398 /// 删除文件或文件夹
399 /// </summary>
400 /// <param name="path">路径</param>
401 /// <param name="filename">名称</param>
402 /// <param name="type">0代表文件夹,1代表文件</param>
403 /// <returns>返回值</returns>
404 public static int Del(string path, string filename, int type)
405 {
406 int result = 0;
407 if (type == 0)
408 {
409 if (Directory.Exists(path.TrimEnd('\\') + "\\" + filename))
410 {
411 try
412 {
413 Directory.Delete(path.TrimEnd('\\') + "\\" + filename, true);
414 }
415 catch (Exception e)
416 {
417 throw new IOException(e.ToString());
418 }
419 result = 1;
420 }
421 else
422 {
423 throw new IOException("参数错误!");
424 }
425 }
426 else
427 {
428 if (File.Exists(path.TrimEnd('\\') + "\\" + filename))
429 {
430 try
431 {
432 File.Delete(path.TrimEnd('\\') + "\\" + filename);
433 }
434 catch (Exception e)
435 {
436 throw new IOException(e.ToString());
437 }
438 result = 1;
439 }
440 else
441 {
442 throw new IOException("参数错误!");
443 }
444 }
445 return result;
446 }
447 #endregion
448 }