DirFileUtil.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Data;
  5. using System.Web;
  6. using System.Reflection;
  7. namespace WMS.Util
  8. {
  9. //string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  10. /// <summary>
  11. /// 描 述:文件夹文件操作类
  12. /// </summary>
  13. public static class DirFileUtil
  14. {
  15. #region 检测指定目录是否存在
  16. /// <summary>
  17. /// 检测指定目录是否存在
  18. /// </summary>
  19. /// <param name="directoryPath">目录的绝对路径</param>
  20. /// <returns></returns>
  21. public static bool IsExistDirectory(string directoryPath)
  22. {
  23. return Directory.Exists(directoryPath);
  24. }
  25. #endregion
  26. #region 获取指定目录中的文件列表
  27. /// <summary>
  28. /// 获取指定目录中所有文件列表
  29. /// </summary>
  30. /// <param name="directoryPath">指定目录的绝对路径</param>
  31. public static string[] GetFileNames(string directoryPath)
  32. {
  33. //如果目录不存在,则抛出异常
  34. if (!IsExistDirectory(directoryPath))
  35. {
  36. throw new FileNotFoundException();
  37. }
  38. //获取文件列表
  39. return Directory.GetFiles(directoryPath);
  40. }
  41. #endregion
  42. #region 获取指定目录中所有子目录列表
  43. /// <summary>
  44. /// 获取指定目录中所有子目录列表
  45. /// </summary>
  46. /// <param name="directoryPath">指定目录的绝对路径</param>
  47. public static string[] GetDirectories(string directoryPath)
  48. {
  49. //如果目录不存在,则抛出异常
  50. if (!IsExistDirectory(directoryPath))
  51. {
  52. throw new FileNotFoundException();
  53. }
  54. return Directory.GetDirectories(directoryPath);
  55. }
  56. #endregion
  57. #region 获取指定目录及子目录中所有文件列表(搜索)
  58. /// <summary>
  59. /// 获取指定目录及子目录中所有文件列表
  60. /// </summary>
  61. /// <param name="directoryPath">指定目录的绝对路径</param>
  62. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  63. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  64. /// <param name="isSearchChild">是否搜索子目录</param>
  65. public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
  66. {
  67. //如果目录不存在,则抛出异常
  68. if (!IsExistDirectory(directoryPath))
  69. {
  70. throw new FileNotFoundException();
  71. }
  72. try
  73. {
  74. if (isSearchChild)
  75. {
  76. return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
  77. }
  78. else
  79. {
  80. return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  81. }
  82. }
  83. catch (IOException ex)
  84. {
  85. throw ex;
  86. }
  87. }
  88. #endregion
  89. #region 根据时间获取指定路径目录下的 指定后缀名的 的所有文件
  90. /// <summary>
  91. /// 根据时间获取指定路径目录下的 指定后缀名的 的所有文件
  92. /// </summary>
  93. /// <param name="path">文件路径</param>
  94. /// <param name="Extension">后缀名 比如.txt</param>
  95. /// <returns></returns>
  96. public static DataRow[] GetFilesByTime(string path, string Extension)
  97. {
  98. if (Directory.Exists(path))
  99. {
  100. string fielExts = string.Format("*{0}", Extension);
  101. string[] files = Directory.GetFiles(path, fielExts);
  102. if (files.Length > 0)
  103. {
  104. DataTable table = new DataTable();
  105. table.Columns.Add(new DataColumn("filename", Type.GetType("System.String")));
  106. table.Columns.Add(new DataColumn("createtime", Type.GetType("System.DateTime")));
  107. for (int i = 0; i < files.Length; i++)
  108. {
  109. DataRow row = table.NewRow();
  110. DateTime creationTime = System.IO.File.GetCreationTime(files[i]);
  111. string fileName = Path.GetFileName(files[i]);
  112. row["filename"] = fileName;
  113. row["createtime"] = creationTime;
  114. table.Rows.Add(row);
  115. }
  116. return table.Select(string.Empty, "createtime desc");
  117. }
  118. }
  119. return new DataRow[0];
  120. }
  121. #endregion
  122. #region 检测指定目录中是否存在指定的文件
  123. /// <summary>
  124. /// 检测指定目录中是否存在指定的文件
  125. /// </summary>
  126. /// <param name="directoryPath">指定目录的绝对路径</param>
  127. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  128. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  129. /// <param name="isSearchChild">是否搜索子目录</param>
  130. public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild = false)
  131. {
  132. try
  133. {
  134. //获取指定的文件列表
  135. string[] fileNames = GetFileNames(directoryPath, searchPattern, isSearchChild);
  136. //判断指定文件是否存在
  137. if (fileNames.Length == 0)
  138. {
  139. return false;
  140. }
  141. else
  142. {
  143. return true;
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. throw new Exception(ex.Message);
  149. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  150. }
  151. }
  152. #endregion
  153. #region 检测指定目录是否为空
  154. /// <summary>
  155. /// 检测指定目录是否为空
  156. /// </summary>
  157. /// <param name="directoryPath">指定目录的绝对路径</param>
  158. public static bool IsEmptyDirectory(string directoryPath)
  159. {
  160. try
  161. {
  162. //判断是否存在文件
  163. string[] fileNames = GetFileNames(directoryPath);
  164. if (fileNames.Length > 0)
  165. {
  166. return false;
  167. }
  168. //判断是否存在文件夹
  169. string[] directoryNames = GetDirectories(directoryPath);
  170. if (directoryNames.Length > 0)
  171. {
  172. return false;
  173. }
  174. return true;
  175. }
  176. catch
  177. {
  178. //这里记录日志
  179. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  180. return true;
  181. }
  182. }
  183. #endregion
  184. #region 创建目录
  185. /// <summary>
  186. /// 创建目录
  187. /// </summary>
  188. /// <param name="dir">要创建的目录路径包括目录名</param>
  189. public static void CreateDir(string dir)
  190. {
  191. if (dir.Length == 0) return;
  192. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  193. if (!Directory.Exists(rootPath + "\\" + dir))
  194. Directory.CreateDirectory(rootPath + "\\" + dir);
  195. }
  196. #endregion
  197. #region 删除目录
  198. /// <summary>
  199. /// 删除目录
  200. /// </summary>
  201. /// <param name="dir">要删除的目录路径和名称</param>
  202. public static void DeleteDir(string dir)
  203. {
  204. if (dir.Length == 0) return;
  205. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  206. if (Directory.Exists(rootPath + "\\" + dir))
  207. Directory.Delete(rootPath + "\\" + dir);
  208. }
  209. #endregion
  210. #region 清空指定目录
  211. /// <summary>
  212. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  213. /// </summary>
  214. /// <param name="directoryPath">指定目录的绝对路径</param>
  215. public static void ClearDirectory(string directoryPath)
  216. {
  217. directoryPath = ConfigHelper.GetValue<string>("baseDir")+ @"/" + directoryPath;
  218. if (IsExistDirectory(directoryPath))
  219. {
  220. //删除目录中所有的文件
  221. string[] fileNames = GetFileNames(directoryPath);
  222. for (int i = 0; i < fileNames.Length; i++)
  223. {
  224. DeleteFile(fileNames[i]);
  225. }
  226. //删除目录中所有的子目录
  227. string[] directoryNames = GetDirectories(directoryPath);
  228. for (int i = 0; i < directoryNames.Length; i++)
  229. {
  230. DeleteDir(directoryNames[i]);
  231. }
  232. }
  233. }
  234. #endregion
  235. #region 复制文件夹
  236. /// <summary>
  237. /// 复制文件夹(递归)
  238. /// </summary>
  239. /// <param name="varFromDirectory">源文件夹路径</param>
  240. /// <param name="varToDirectory">目标文件夹路径</param>
  241. public static void CopyFolder(string varFromDirectory, string varToDirectory)
  242. {
  243. Directory.CreateDirectory(varToDirectory);
  244. if (!Directory.Exists(varFromDirectory)) return;
  245. string[] directories = Directory.GetDirectories(varFromDirectory);
  246. if (directories.Length > 0)
  247. {
  248. foreach (string d in directories)
  249. {
  250. CopyFolder(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
  251. }
  252. }
  253. string[] files = Directory.GetFiles(varFromDirectory);
  254. if (files.Length > 0)
  255. {
  256. foreach (string s in files)
  257. {
  258. System.IO.File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\")), true);
  259. }
  260. }
  261. }
  262. #endregion
  263. #region 检测指定文件是否存在,如果存在返回true
  264. /// <summary>
  265. /// 检测指定文件是否存在,如果存在则返回true。
  266. /// </summary>
  267. /// <param name="filePath">文件的绝对路径</param>
  268. public static bool IsExistFile(string filePath)
  269. {
  270. return File.Exists(filePath);
  271. }
  272. /// <summary>
  273. /// 检测指定文件是否存在,如果存在则返回true。
  274. /// </summary>
  275. /// <param name="filePath">文件的相对路径</param>
  276. /// <returns></returns>
  277. public static bool IsExistFileEx(string filePath)
  278. {
  279. string rootPath = Assembly.GetExecutingAssembly().CodeBase.Replace("/bin/Learun.Util.DLL", "").Replace("file:///", "");
  280. return File.Exists(rootPath + filePath);
  281. }
  282. #endregion
  283. #region 创建文件
  284. /// <summary>
  285. /// 创建文件
  286. /// </summary>
  287. /// <param name="dir">相对路径</param>
  288. /// <param name="pagestr">文件内容</param>
  289. public static void CreateFile(string dir, string pagestr)
  290. {
  291. dir = dir.Replace("/", "\\");
  292. if (dir.IndexOf("\\") > -1)
  293. CreateDir(dir.Substring(0, dir.LastIndexOf("\\")));
  294. System.IO.StreamWriter sw = new System.IO.StreamWriter(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir, false, System.Text.Encoding.GetEncoding("utf-8"));
  295. sw.Write(pagestr);
  296. sw.Close();
  297. }
  298. /// <summary>
  299. /// 创建一个文件。
  300. /// </summary>
  301. /// <param name="filePath">文件的绝对路径</param>
  302. public static void CreateFile(string filePath)
  303. {
  304. try
  305. {
  306. //如果文件不存在则创建该文件
  307. if (!IsExistFile(filePath))
  308. {
  309. //创建一个FileInfo对象
  310. FileInfo file = new FileInfo(filePath);
  311. //创建文件
  312. FileStream fs = file.Create();
  313. //关闭文件流
  314. fs.Close();
  315. }
  316. }
  317. catch (Exception ex)
  318. {
  319. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  320. throw ex;
  321. }
  322. }
  323. /// <summary>
  324. /// 创建一个文件,并将字节流写入文件。
  325. /// </summary>
  326. /// <param name="filePath">文件的绝对路径</param>
  327. /// <param name="buffer">二进制流数据</param>
  328. public static void CreateFile(string filePath, byte[] buffer)
  329. {
  330. try
  331. {
  332. //如果文件不存在则创建该文件
  333. if (!IsExistFile(filePath))
  334. {
  335. //创建一个FileInfo对象
  336. FileInfo file = new FileInfo(filePath);
  337. //创建文件
  338. FileStream fs = file.Create();
  339. //写入二进制流
  340. fs.Write(buffer, 0, buffer.Length);
  341. //关闭文件流
  342. fs.Close();
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  348. throw ex;
  349. }
  350. }
  351. /// <summary>
  352. /// 创建文件
  353. /// </summary>
  354. /// <param name="path">绝对路径</param>
  355. /// <param name="content">内容</param>
  356. public static void CreateFileContent(string path, string content)
  357. {
  358. FileInfo fi = new FileInfo(path);
  359. var di = fi.Directory;
  360. if (!di.Exists)
  361. {
  362. di.Create();
  363. }
  364. StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
  365. sw.Write(content);
  366. sw.Close();
  367. }
  368. /// <summary>
  369. /// 创建文件
  370. /// </summary>
  371. /// <param name="path">绝对路径</param>
  372. /// <param name="content">内容</param>
  373. /// <param name="encoding">编码格式</param>
  374. public static void CreateFileContent(string path, string content, string encoding)
  375. {
  376. FileInfo fi = new FileInfo(path);
  377. var di = fi.Directory;
  378. if (!di.Exists)
  379. {
  380. di.Create();
  381. }
  382. StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding(encoding));
  383. sw.Write(content);
  384. sw.Close();
  385. }
  386. #endregion
  387. #region 删除文件
  388. /// <summary>
  389. /// 删除文件
  390. /// </summary>
  391. /// <param name="file">要删除的文件路径和名称</param>
  392. public static void DeleteFile(string file)
  393. {
  394. if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir")+@"\" + file))
  395. {
  396. System.IO.File.Delete(ConfigHelper.GetValue<string>("baseDir") + @"\" + file);
  397. }
  398. }
  399. #endregion
  400. #region 移动文件(剪贴--粘贴)
  401. /// <summary>
  402. /// 移动文件(剪贴--粘贴)
  403. /// </summary>
  404. /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param>
  405. /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param>
  406. public static void MoveFile(string dir1, string dir2)
  407. {
  408. dir1 = dir1.Replace("/", "\\");
  409. dir2 = dir2.Replace("/", "\\");
  410. if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1))
  411. System.IO.File.Move(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1, ConfigHelper.GetValue<string>("baseDir") + "\\" + dir2);
  412. }
  413. #endregion
  414. #region 复制文件
  415. /// <summary>
  416. /// 复制文件
  417. /// </summary>
  418. /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param>
  419. /// <param name="dir2">目标位置,并指定新的文件名</param>
  420. public static void CopyFile(string dir1, string dir2)
  421. {
  422. dir1 = dir1.Replace("/", "\\");
  423. dir2 = dir2.Replace("/", "\\");
  424. if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1))
  425. {
  426. System.IO.File.Copy(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1, ConfigHelper.GetValue<string>("baseDir") + "\\" + dir2, true);
  427. }
  428. }
  429. #endregion
  430. #region 检查文件,如果文件不存在则创建
  431. /// <summary>
  432. /// 检查文件,如果文件不存在则创建
  433. /// </summary>
  434. /// <param name="FilePath">路径,包括文件名</param>
  435. public static void ExistsFile(string FilePath)
  436. {
  437. //if(!File.Exists(FilePath))
  438. //File.Create(FilePath);
  439. //以上写法会报错,详细解释请看下文.........
  440. if (!System.IO.File.Exists(FilePath))
  441. {
  442. FileStream fs = System.IO.File.Create(FilePath);
  443. fs.Close();
  444. }
  445. }
  446. #endregion
  447. #region 删除指定文件夹对应其他文件夹里的文件
  448. /// <summary>
  449. /// 删除指定文件夹对应其他文件夹里的文件
  450. /// </summary>
  451. /// <param name="varFromDirectory">指定文件夹路径</param>
  452. /// <param name="varToDirectory">对应其他文件夹路径</param>
  453. public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory)
  454. {
  455. Directory.CreateDirectory(varToDirectory);
  456. if (!Directory.Exists(varFromDirectory)) return;
  457. string[] directories = Directory.GetDirectories(varFromDirectory);
  458. if (directories.Length > 0)
  459. {
  460. foreach (string d in directories)
  461. {
  462. DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
  463. }
  464. }
  465. string[] files = Directory.GetFiles(varFromDirectory);
  466. if (files.Length > 0)
  467. {
  468. foreach (string s in files)
  469. {
  470. System.IO.File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\\")));
  471. }
  472. }
  473. }
  474. #endregion
  475. #region 从文件的绝对路径中获取文件名( 包含扩展名 )
  476. /// <summary>
  477. /// 从文件的绝对路径中获取文件名( 包含扩展名 )
  478. /// </summary>
  479. /// <param name="filePath">文件的绝对路径</param>
  480. public static string GetFileName(string filePath)
  481. {
  482. //获取文件的名称
  483. FileInfo fi = new FileInfo(filePath);
  484. return fi.Name;
  485. }
  486. #endregion
  487. #region 复制文件参考方法,页面中引用
  488. /// <summary>
  489. /// 复制文件参考方法,页面中引用
  490. /// </summary>
  491. /// <param name="cDir">新路径</param>
  492. /// <param name="TempId">模板引擎替换编号</param>
  493. public static void CopyFiles(string cDir, string TempId)
  494. {
  495. //if (Directory.Exists(Request.PhysicalApplicationPath + "\\Controls"))
  496. //{
  497. // string TempStr = string.Empty;
  498. // StreamWriter sw;
  499. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Default.aspx"))
  500. // {
  501. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Default.aspx");
  502. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  503. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Default.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  504. // sw.Write(TempStr);
  505. // sw.Close();
  506. // }
  507. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Column.aspx"))
  508. // {
  509. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Column.aspx");
  510. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  511. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\List.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  512. // sw.Write(TempStr);
  513. // sw.Close();
  514. // }
  515. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Content.aspx"))
  516. // {
  517. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Content.aspx");
  518. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  519. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\View.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  520. // sw.Write(TempStr);
  521. // sw.Close();
  522. // }
  523. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\MoreDiss.aspx"))
  524. // {
  525. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\MoreDiss.aspx");
  526. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  527. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\DissList.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  528. // sw.Write(TempStr);
  529. // sw.Close();
  530. // }
  531. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\ShowDiss.aspx"))
  532. // {
  533. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\ShowDiss.aspx");
  534. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  535. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Diss.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  536. // sw.Write(TempStr);
  537. // sw.Close();
  538. // }
  539. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Review.aspx"))
  540. // {
  541. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Review.aspx");
  542. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  543. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Review.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  544. // sw.Write(TempStr);
  545. // sw.Close();
  546. // }
  547. // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Search.aspx"))
  548. // {
  549. // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Search.aspx");
  550. // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
  551. // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Search.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
  552. // sw.Write(TempStr);
  553. // sw.Close();
  554. // }
  555. //}
  556. }
  557. #endregion
  558. #region 获取文本文件的行数
  559. /// <summary>
  560. /// 获取文本文件的行数
  561. /// </summary>
  562. /// <param name="filePath">文件的绝对路径</param>
  563. public static int GetLineCount(string filePath)
  564. {
  565. //将文本文件的各行读到一个字符串数组中
  566. string[] rows = System.IO.File.ReadAllLines(filePath);
  567. //返回行数
  568. return rows.Length;
  569. }
  570. #endregion
  571. #region 获取一个文件的长度
  572. /// <summary>
  573. /// 获取一个文件的长度,单位为Byte
  574. /// </summary>
  575. /// <param name="filePath">文件的绝对路径</param>
  576. public static int GetFileSize(string filePath)
  577. {
  578. //创建一个文件对象
  579. FileInfo fi = new FileInfo(filePath);
  580. //获取文件的大小
  581. return (int)fi.Length;
  582. }
  583. #endregion
  584. #region 获取指定目录中的子目录列表
  585. /// <summary>
  586. /// 获取指定目录及子目录中所有子目录列表
  587. /// </summary>
  588. /// <param name="directoryPath">指定目录的绝对路径</param>
  589. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  590. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  591. /// <param name="isSearchChild">是否搜索子目录</param>
  592. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  593. {
  594. try
  595. {
  596. if (isSearchChild)
  597. {
  598. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
  599. }
  600. else
  601. {
  602. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  603. }
  604. }
  605. catch (IOException ex)
  606. {
  607. throw ex;
  608. }
  609. }
  610. #endregion
  611. #region 向文本文件写入内容
  612. /// <summary>
  613. /// 向文本文件中写入内容
  614. /// </summary>
  615. /// <param name="filePath">文件的绝对路径</param>
  616. /// <param name="text">写入的内容</param>
  617. /// <param name="encoding">编码</param>
  618. public static void WriteText(string filePath, string text, Encoding encoding)
  619. {
  620. //向文件写入内容
  621. System.IO.File.WriteAllText(filePath, text, encoding);
  622. }
  623. #endregion
  624. #region 向文本文件的尾部追加内容
  625. /// <summary>
  626. /// 向文本文件的尾部追加内容
  627. /// </summary>
  628. /// <param name="filePath">文件的绝对路径</param>
  629. /// <param name="content">写入的内容</param>
  630. public static void AppendText(string filePath, string content)
  631. {
  632. System.IO.File.AppendAllText(filePath, content);
  633. }
  634. #endregion
  635. #region 将现有文件的内容复制到新文件中
  636. /// <summary>
  637. /// 将源文件的内容复制到目标文件中
  638. /// </summary>
  639. /// <param name="sourceFilePath">源文件的绝对路径</param>
  640. /// <param name="destFilePath">目标文件的绝对路径</param>
  641. public static void Copy(string sourceFilePath, string destFilePath)
  642. {
  643. System.IO.File.Copy(sourceFilePath, destFilePath, true);
  644. }
  645. #endregion
  646. #region 将文件移动到指定目录
  647. /// <summary>
  648. /// 将文件移动到指定目录
  649. /// </summary>
  650. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  651. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  652. public static void Move(string sourceFilePath, string descDirectoryPath)
  653. {
  654. //获取源文件的名称
  655. string sourceFileName = GetFileName(sourceFilePath);
  656. if (IsExistDirectory(descDirectoryPath))
  657. {
  658. //如果目标中存在同名文件,则删除
  659. if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
  660. {
  661. DeleteFile(descDirectoryPath + "\\" + sourceFileName);
  662. }
  663. //将文件移动到指定目录
  664. System.IO.File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
  665. }
  666. }
  667. #endregion
  668. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  669. /// <summary>
  670. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  671. /// </summary>
  672. /// <param name="filePath">文件的绝对路径</param>
  673. public static string GetFileNameNoExtension(string filePath)
  674. {
  675. //获取文件的名称
  676. FileInfo fi = new FileInfo(filePath);
  677. return fi.Name.Split('.')[0];
  678. }
  679. #endregion
  680. #region 从文件的绝对路径中获取扩展名
  681. /// <summary>
  682. /// 从文件的绝对路径中获取扩展名
  683. /// </summary>
  684. /// <param name="filePath">文件的绝对路径</param>
  685. public static string GetExtension(string filePath)
  686. {
  687. //获取文件的名称
  688. FileInfo fi = new FileInfo(filePath);
  689. return fi.Extension;
  690. }
  691. #endregion
  692. #region 清空文件内容
  693. /// <summary>
  694. /// 清空文件内容
  695. /// </summary>
  696. /// <param name="filePath">文件的绝对路径</param>
  697. public static void ClearFile(string filePath)
  698. {
  699. //删除文件
  700. System.IO.File.Delete(filePath);
  701. //重新创建该文件
  702. CreateFile(filePath);
  703. }
  704. #endregion
  705. #region 获取文件大小并以B,KB,GB,TB
  706. /// <summary>
  707. /// 计算文件大小函数(保留两位小数),Size为字节大小
  708. /// </summary>
  709. /// <param name="size">初始文件大小</param>
  710. /// <returns></returns>
  711. public static string ToFileSize(long size)
  712. {
  713. string m_strSize = "";
  714. long FactSize = 0;
  715. FactSize = size;
  716. if (FactSize < 1024.00)
  717. m_strSize = FactSize.ToString("F2") + " 字节";
  718. else if (FactSize >= 1024.00 && FactSize < 1048576)
  719. m_strSize = (FactSize / 1024.00).ToString("F2") + " KB";
  720. else if (FactSize >= 1048576 && FactSize < 1073741824)
  721. m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " MB";
  722. else if (FactSize >= 1073741824)
  723. m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " GB";
  724. return m_strSize;
  725. }
  726. #endregion
  727. #region 返回绝对路径
  728. /// <summary>
  729. /// 返回绝对路径
  730. /// </summary>
  731. /// <param name="filePath">相对路径</param>
  732. /// <returns></returns>
  733. public static string GetAbsolutePath(string filePath)
  734. {
  735. return ConfigHelper.GetValue<string>("baseDir") + @"\" + filePath;
  736. }
  737. #endregion
  738. }
  739. }