FileOperate.cs 24 KB

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