1 using System.Data;
2 using System.Configuration;
3 using System.Web;
4 using System.Web.Security;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Web.UI.WebControls.WebParts;
8 using System.Web.UI.HtmlControls;
9 using System.Text;
10 using System.IO;
11
12 namespace EC
13 {
14 /**////
15 /// FileObj 的摘要说明
16 ///
17 public class FileObj
18 {
19 构造函数#region 构造函数
20 private bool _alreadyDispose = false;
21 public FileObj()
22 {
23 //
24 // TODO: 在此处添加构造函数逻辑
25 //
26 }
27 ~FileObj()
28 {
29 Dispose(); ;
30 }
31
32 protected virtual void Dispose(bool isDisposing)
33 {
34 if (_alreadyDispose) return;
35 //if (isDisposing)
36 //{
37 // if (xml != null)
38 // {
39 // xml = null;
40 // }
41 //}
42 _alreadyDispose = true;
43 }
44 #endregion
45
46 IDisposable 成员#region IDisposable 成员
47
48 public void Dispose()
49 {
50 Dispose(true);
51 GC.SuppressFinalize(this);
52 }
53
54 #endregion
55
56 取得文件后缀名#region 取得文件后缀名
57 /**//****************************************
58 * 函数名称:GetPostfixStr
59 * 功能说明:取得文件后缀名
60 * 参 数:filename:文件名称
61 * 调用示列:
62 * string filename = "aaa.aspx";
63 * string s = EC.FileObj.GetPostfixStr(filename);
64 *****************************************/
65 /**////
66 /// 取后缀名
67 ///
68 /// 文件名
69 /// .gif|.html格式
70 public static string GetPostfixStr(string filename)
71 {
72 int start = filename.LastIndexOf(".");
73 int length = filename.Length;
74 string postfix = filename.Substring(start, length - start);
75 return postfix;
76 }
77 #endregion
78
79 写文件#region 写文件
80 /**//****************************************
81 * 函数名称:WriteFile
82 * 功能说明:当文件不存时,则创建文件,并追加文件
83 * 参 数:Path:文件路径,Strings:文本内容
84 * 调用示列:
85 * string Path = Server.MapPath("Default2.aspx");
86 * string Strings = "这是我写的内容啊";
87 * EC.FileObj.WriteFile(Path,Strings);
88 *****************************************/
89 /**////
90 /// 写文件
91 ///
92 /// 文件路径
93 /// 文件内容
94 public static void WriteFile(string Path, string Strings)
95 {
96
97 if (!System.IO.File.Exists(Path))
98 {
99 //Directory.CreateDirectory(Path);
100
101 System.IO.FileStream f = System.IO.File.Create(Path);
102 f.Close();
103 f.Dispose();
104 }
105 System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
106 f2.WriteLine(Strings);
107 f2.Close();
108 f2.Dispose();
109
110
111 }
112 #endregion
113
114 读文件#region 读文件
115 /**//****************************************
116 * 函数名称:ReadFile
117 * 功能说明:读取文本内容
118 * 参 数:Path:文件路径
119 * 调用示列:
120 * string Path = Server.MapPath("Default2.aspx");
121 * string s = EC.FileObj.ReadFile(Path);
122 *****************************************/
123 /**////
124 /// 读文件
125 ///
126 /// 文件路径
127 ///
128 public static string ReadFile(string Path)
129 {
130 string s = "";
131 if (!System.IO.File.Exists(Path))
132 s = "不存在相应的目录";
133 else
134 {
135 StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
136 s = f2.ReadToEnd();
137 f2.Close();
138 f2.Dispose();
139 }
140
141 return s;
142 }
143 #endregion
144
145 追加文件#region 追加文件
146 /**//****************************************
147 * 函数名称:FileAdd
148 * 功能说明:追加文件内容
149 * 参 数:Path:文件路径,strings:内容
150 * 调用示列:
151 * string Path = Server.MapPath("Default2.aspx");
152 * string Strings = "新追加内容";
153 * EC.FileObj.FileAdd(Path, Strings);
154 *****************************************/
155 /**////
156 /// 追加文件
157 ///
158 /// 文件路径
159 /// 内容
160 public static void FileAdd(string Path, string strings)
161 {
162 StreamWriter sw = File.AppendText(Path);
163 sw.Write(strings);
164 sw.Flush();
165 sw.Close();
166 sw.Dispose();
167 }
168 #endregion
169
170 拷贝文件#region 拷贝文件
171 /**//****************************************
172 * 函数名称:FileCoppy
173 * 功能说明:拷贝文件
174 * 参 数:OrignFile:原始文件,NewFile:新文件路径
175 * 调用示列:
176 * string OrignFile = Server.MapPath("Default2.aspx");
177 * string NewFile = Server.MapPath("Default3.aspx");
178 * EC.FileObj.FileCoppy(OrignFile, NewFile);
179 *****************************************/
180 /**////
181 /// 拷贝文件
182 ///
183 /// 原始文件
184 /// 新文件路径
185 public static void FileCoppy(string OrignFile, string NewFile)
186 {
187 File.Copy(OrignFile, NewFile, true);
188 }
189
190 #endregion
191
192 删除文件#region 删除文件
193 /**//****************************************
194 * 函数名称:FileDel
195 * 功能说明:删除文件
196 * 参 数:Path:文件路径
197 * 调用示列:
198 * string Path = Server.MapPath("Default3.aspx");
199 * EC.FileObj.FileDel(Path);
200 *****************************************/
201 /**////
202 /// 删除文件
203 ///
204 /// 路径
205 public static void FileDel(string Path)
206 {
207 File.Delete(Path);
208 }
209 #endregion
210
211 移动文件#region 移动文件
212 /**//****************************************
213 * 函数名称:FileMove
214 * 功能说明:移动文件
215 * 参 数:OrignFile:原始路径,NewFile:新文件路径
216 * 调用示列:
217 * string OrignFile = Server.MapPath("../说明.txt");
218 * string NewFile = Server.MapPath("http://www.cnblogs.com/说明.txt");
219 * EC.FileObj.FileMove(OrignFile, NewFile);
220 *****************************************/
221 /**////
222 /// 移动文件
223 ///
224 /// 原始路径
225 /// 新路径
226 public static void FileMove(string OrignFile, string NewFile)
227 {
228 File.Move(OrignFile, NewFile);
229 }
230 #endregion
231
232 在当前目录下创建目录#region 在当前目录下创建目录
233 /**//****************************************
234 * 函数名称:FolderCreate
235 * 功能说明:在当前目录下创建目录
236 * 参 数:OrignFolder:当前目录,NewFloder:新目录
237 * 调用示列:
238 * string OrignFolder = Server.MapPath("test/");
239 * string NewFloder = "new";
240 * EC.FileObj.FolderCreate(OrignFolder, NewFloder);
241 *****************************************/
242 /**////
243 /// 在当前目录下创建目录
244 ///
245 /// 当前目录
246 /// 新目录
247 public static void FolderCreate(string OrignFolder, string NewFloder)
248 {
249 Directory.SetCurrentDirectory(OrignFolder);
250 Directory.CreateDirectory(NewFloder);
251 }
252
253 /**////
254 /// 创建文件夹
255 ///
256 ///
257 public static void FolderCreate(string Path)
258 {
259 // 判断目标目录是否存在如果不存在则新建之
260 if (!Directory.Exists(Path))
261 Directory.CreateDirectory(Path);
262 }
263
264 #endregion
265
266 创建目录#region 创建目录
267 public static void FileCreate(string Path)
268 {
269 FileInfo CreateFile = new FileInfo(Path); //创建文件
270 if (!CreateFile.Exists)
271 {
272 FileStream FS = CreateFile.Create();
273 FS.Close();
274 }
275 }
276 #endregion
277
278 递归删除文件夹目录及文件#region 递归删除文件夹目录及文件
279 /**//****************************************
280 * 函数名称:DeleteFolder
281 * 功能说明:递归删除文件夹目录及文件
282 * 参 数:dir:文件夹路径
283 * 调用示列:
284 * string dir = Server.MapPath("test/");
285 * EC.FileObj.DeleteFolder(dir);
286 *****************************************/
287 /**////
288 /// 递归删除文件夹目录及文件
289 ///
290 ///
291 ///
292 public static void DeleteFolder(string dir)
293 {
294 if (Directory.Exists(dir)) //如果存在这个文件夹删除之
295 {
296 foreach (string d in Directory.GetFileSystemEntries(dir))
297 {
298 if (File.Exists(d))
299 File.Delete(d); //直接删除其中的文件
300 else
301 DeleteFolder(d); //递归删除子文件夹
302 }
303 Directory.Delete(dir, true); //删除已空文件夹
304 }
305 }
306
307 #endregion
308
309 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。#region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
310 /**//****************************************
311 * 函数名称:CopyDir
312 * 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
313 * 参 数:srcPath:原始路径,aimPath:目标文件夹
314 * 调用示列:
315 * string srcPath = Server.MapPath("test/");
316 * string aimPath = Server.MapPath("test1/");
317 * EC.FileObj.CopyDir(srcPath,aimPath);
318 *****************************************/
319 /**////
320 /// 指定文件夹下面的所有内容copy到目标文件夹下面
321 ///
322 /// 原始路径
323 /// 目标文件夹
324 public static void CopyDir(string srcPath, string aimPath)
325 {
326 try
327 {
328 // 检查目标目录是否以目录分割字符结束如果不是则添加之
329 if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
330 aimPath += Path.DirectorySeparatorChar;
331 // 判断目标目录是否存在如果不存在则新建之
332 if (!Directory.Exists(aimPath))
333 Directory.CreateDirectory(aimPath);
334 // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
335 //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
336 //string[] fileList = Directory.GetFiles(srcPath);
337 string[] fileList = Directory.GetFileSystemEntries(srcPath);
338 //遍历所有的文件和目录
339 foreach (string file in fileList)
340 {
341 //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
342
343 if (Directory.Exists(file))
344 CopyDir(file, aimPath + Path.GetFileName(file));
345 //否则直接Copy文件
346 else
347 File.Copy(file, aimPath + Path.GetFileName(file), true);
348 }
349 }
350 catch (Exception ee)
351 {
352 throw new Exception(ee.ToString());
353 }
354 }
355 #endregion
356
357 获取指定文件夹下所有子目录及文件(树形)#region 获取指定文件夹下所有子目录及文件(树形)
358 /**//****************************************
359 * 函数名称:GetFoldAll(string Path)
360 * 功能说明:获取指定文件夹下所有子目录及文件(树形)
361 * 参 数:Path:详细路径
362 * 调用示列:
363 * string strDirlist = Server.MapPath("templates");
364 * this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);
365 *****************************************/
366 /**////
367 /// 获取指定文件夹下所有子目录及文件
368 ///
369 /// 详细路径
370 public static string GetFoldAll(string Path)
371 {
372
373 string str = "";
374 DirectoryInfo thisOne = new DirectoryInfo(Path);
375 str = ListTreeShow(thisOne, 0, str);
376 return str;
377
378 }
379
380 /**////
381 /// 获取指定文件夹下所有子目录及文件函数
382 ///
383 /// 指定目录
384 /// 默认起始值,调用时,一般为0
385 /// 用于迭加的传入值,一般为空
386 ///
387 public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//递归目录 文件
388 {
389 DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
390 foreach (DirectoryInfo dirinfo in subDirectories)
391 {
392
393 if (nLevel == 0)
394 {
395 Rn += "├";
396 }
397 else
398 {
399 string _s = "";
400 for (int i = 1; i <= nLevel; i++)
401 {
402 _s += "│ ";
403 }
404 Rn += _s + "├";
405 }
406 Rn += "" + dirinfo.Name.ToString() + "
";
407 FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件
408 foreach (FileInfo fInfo in fileInfo)
409 {
410 if (nLevel == 0)
411 {
412 Rn += "│ ├";
413 }
414 else
415 {
416 string _f = "";
417 for (int i = 1; i <= nLevel; i++)
418 {
419 _f += "│ ";
420 }
421 Rn += _f + "│ ├";
422 }
423 Rn += fInfo.Name.ToString() + "
";
424 }
425 Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);
426
427
428 }
429 return Rn;
430 }
431
432
433
434 /**//****************************************
435 * 函数名称:GetFoldAll(string Path)
436 * 功能说明:获取指定文件夹下所有子目录及文件(下拉框形)
437 * 参 数:Path:详细路径
438 * 调用示列:
439 * string strDirlist = Server.MapPath("templates");
440 * this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");
441 *****************************************/
442 /**////
443 /// 获取指定文件夹下所有子目录及文件(下拉框形)
444 ///
445 /// 详细路径
446 ///下拉列表名称
447 ///默认选择模板名称
448 public static string GetFoldAll(string Path,string DropName,string tplPath)
449 {
450 string strDrop = "";
455
456 }
457
458 /**////
459 /// 获取指定文件夹下所有子目录及文件函数
460 ///
461 /// 指定目录
462 /// 默认起始值,调用时,一般为0
463 /// 用于迭加的传入值,一般为空
464 /// 默认选择模板名称
465 ///
466 public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//递归目录 文件
467 {
468 DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
469
470 foreach (DirectoryInfo dirinfo in subDirectories)
471 {
472
473 Rn += "";
494
495
496 FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件
497 foreach (FileInfo fInfo in fileInfo)
498 {
499 Rn += "";
520 }
521 Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);
522
523
524 }
525 return Rn;
526 }
527 #endregion
528
529 获取文件夹大小#region 获取文件夹大小
530 /**//****************************************
531 * 函数名称:GetDirectoryLength(string dirPath)
532 * 功能说明:获取文件夹大小
533 * 参 数:dirPath:文件夹详细路径
534 * 调用示列:
535 * string Path = Server.MapPath("templates");
536 * Response.Write(EC.FileObj.GetDirectoryLength(Path));
537 *****************************************/
538 /**////
539 /// 获取文件夹大小
540 ///
541 /// 文件夹路径
542 ///
543 public static long GetDirectoryLength(string dirPath)
544 {
545 if (!Directory.Exists(dirPath))
546 return 0;
547 long len = 0;
548 DirectoryInfo di = new DirectoryInfo(dirPath);
549 foreach (FileInfo fi in di.GetFiles())
550 {
551 len += fi.Length;
552 }
553 DirectoryInfo[] dis = di.GetDirectories();
554 if (dis.Length > 0)
555 {
556 for (int i = 0; i < dis.Length; i++)
557 {
558 len += GetDirectoryLength(dis.FullName);
559 }
560 }
561 return len;
562 }
563 #endregion
564
565 获取指定文件详细属性#region 获取指定文件详细属性
566 /**//****************************************
567 * 函数名称:GetFileAttibe(string filePath)
568 * 功能说明:获取指定文件详细属性
569 * 参 数:filePath:文件详细路径
570 * 调用示列:
571 * string file = Server.MapPath("robots.txt");
572 * Response.Write(EC.FileObj.GetFileAttibe(file));
573 *****************************************/
574 /**////
575 /// 获取指定文件详细属性
576 ///
577 /// 文件详细路径
578 ///
579 public static string GetFileAttibe(string filePath)
580 {
581 string str = "";
582 System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
583 str += "详细路径:" + objFI.FullName + "
文件名称:" + objFI.Name + "
文件长度:" + objFI.Length.ToString() + "字节
创建时间" + objFI.CreationTime.ToString() + "
最后访问时间:" + objFI.LastAccessTime.ToString() + "
修改时间:" + objFI.LastWriteTime.ToString() + "
所在目录:" + objFI.DirectoryName + "
扩展名:" + objFI.Extension;
584 return str;
585 }
586 #endregion
587 }
588 }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-622782/
,如需转载,请注明出处,否则将追究法律责任。