FileOperate.cs 22 KB

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