18位长度的计时周期数: DateTime.Now.Ticks.ToString()

多数是收集而来,加上测试感觉很不错,分享一下或许有些帮助吧:

引用:

  1. using System;
  2. using System.Text;
  3. using System.IO;

主代码:

  1. namespace PorjectTools
  2. {
  3. ///<summary>
  4. ///</summary>
  5. public static class FileHelper
  6. {
  7. #region 检测指定目录是否存在
  8. /// <summary>
  9. /// 检测指定目录是否存在
  10. /// </summary>
  11. /// <param name="directoryPath">目录的绝对路径</param>
  12. public static bool IsExistDirectory(string directoryPath)
  13. {
  14. return Directory.Exists(directoryPath);
  15. }
  16. #endregion
  17. #region 检测指定文件是否存在
  18. /// <summary>
  19. /// 检测指定文件是否存在,如果存在则返回true。
  20. /// </summary>
  21. /// <param name="filePath">文件的绝对路径</param>
  22. public static bool IsExistFile(string filePath)
  23. {
  24. return File.Exists(filePath);
  25. }
  26. #endregion
  27. #region 检测指定目录是否为空
  28. /// <summary>
  29. /// 检测指定目录是否为空
  30. /// </summary>
  31. /// <param name="directoryPath">指定目录的绝对路径</param>
  32. public static bool IsEmptyDirectory(string directoryPath)
  33. {
  34. try
  35. {
  36. //判断是否存在文件
  37. string[] fileNames = GetFileNames(directoryPath);
  38. if (fileNames.Length > 0)
  39. {
  40. return false;
  41. }
  42. //判断是否存在文件夹
  43. string[] directoryNames = GetDirectories(directoryPath);
  44. return directoryNames.Length <= 0;
  45. }
  46. catch
  47. {
  48. return false;
  49. }
  50. }
  51. #endregion
  52. #region 检测指定目录中是否存在指定的文件
  53. /// <summary>
  54. /// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
  55. /// </summary>
  56. /// <param name="directoryPath">指定目录的绝对路径</param>
  57. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  58. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  59. public static bool Contains(string directoryPath, string searchPattern)
  60. {
  61. try
  62. {
  63. //获取指定的文件列表
  64. string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
  65. //判断指定文件是否存在
  66. return fileNames.Length != 0;
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. }
  73. /// <summary>
  74. /// 检测指定目录中是否存在指定的文件
  75. /// </summary>
  76. /// <param name="directoryPath">指定目录的绝对路径</param>
  77. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  78. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  79. /// <param name="isSearchChild">是否搜索子目录</param>
  80. public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
  81. {
  82. try
  83. {
  84. //获取指定的文件列表
  85. string[] fileNames = GetFileNames(directoryPath, searchPattern, true);
  86. //判断指定文件是否存在
  87. return fileNames.Length != 0;
  88. }
  89. catch
  90. {
  91. return false;
  92. }
  93. }
  94. #endregion
  95. #region 创建一个目录
  96. /// <summary>
  97. /// 创建一个目录
  98. /// </summary>
  99. /// <param name="directoryPath">目录的绝对路径</param>
  100. public static void CreateDirectory(string directoryPath)
  101. {
  102. //如果目录不存在则创建该目录
  103. if (!IsExistDirectory(directoryPath))
  104. {
  105. Directory.CreateDirectory(directoryPath);
  106. }
  107. }
  108. #endregion
  109. #region 创建一个文件
  110. /// <summary>
  111. /// 创建一个文件。
  112. /// </summary>
  113. /// <param name="filePath">文件的绝对路径</param>
  114. public static bool CreateFile(string filePath)
  115. {
  116. try
  117. {
  118. //如果文件不存在则创建该文件
  119. if (!IsExistFile(filePath))
  120. {
  121. //创建一个FileInfo对象
  122. FileInfo file = new FileInfo(filePath);
  123. //创建文件
  124. FileStream fs = file.Create();
  125. //关闭文件流
  126. fs.Close();
  127. }
  128. }
  129. catch
  130. {
  131. return false;
  132. }
  133. return true;
  134. }
  135. /// <summary>
  136. /// 创建一个文件,并将字节流写入文件。
  137. /// </summary>
  138. /// <param name="filePath">文件的绝对路径</param>
  139. /// <param name="buffer">二进制流数据</param>
  140. public static bool CreateFile(string filePath, byte[] buffer)
  141. {
  142. try
  143. {
  144. //如果文件不存在则创建该文件
  145. if (!IsExistFile(filePath))
  146. {
  147. //创建一个FileInfo对象
  148. FileInfo file = new FileInfo(filePath);
  149. //创建文件
  150. FileStream fs = file.Create();
  151. //写入二进制流
  152. fs.Write(buffer, 0, buffer.Length);
  153. //关闭文件流
  154. fs.Close();
  155. }
  156. }
  157. catch
  158. {
  159. return false;
  160. }
  161. return true;
  162. }
  163. #endregion
  164. #region 获取文本文件的行数
  165. /// <summary>
  166. /// 获取文本文件的行数
  167. /// </summary>
  168. /// <param name="filePath">文件的绝对路径</param>
  169. public static int GetLineCount(string filePath)
  170. {
  171. //将文本文件的各行读到一个字符串数组中
  172. string[] rows = File.ReadAllLines(filePath);
  173. //返回行数
  174. return rows.Length;
  175. }
  176. #endregion
  177. #region 获取一个文件的长度
  178. /// <summary>
  179. /// 获取一个文件的长度,单位为Byte
  180. /// </summary>
  181. /// <param name="filePath">文件的绝对路径</param>
  182. public static int GetFileSize(string filePath)
  183. {
  184. //创建一个文件对象
  185. FileInfo fi = new FileInfo(filePath);
  186. //获取文件的大小
  187. return (int)fi.Length;
  188. }
  189. /// <summary>
  190. /// 获取一个文件的长度,单位为KB
  191. /// </summary>
  192. /// <param name="filePath">文件的路径</param>
  193. public static double GetFileSizeByKB(string filePath)
  194. {
  195. //创建一个文件对象
  196. FileInfo fi = new FileInfo(filePath);
  197. long size = fi.Length / 1024;
  198. //获取文件的大小
  199. return double.Parse(size.ToString());
  200. }
  201. /// <summary>
  202. /// 获取一个文件的长度,单位为MB
  203. /// </summary>
  204. /// <param name="filePath">文件的路径</param>
  205. public static double GetFileSizeByMB(string filePath)
  206. {
  207. //创建一个文件对象
  208. FileInfo fi = new FileInfo(filePath);
  209. long size = fi.Length / 1024 / 1024;
  210. //获取文件的大小
  211. return double.Parse(size.ToString());
  212. }
  213. #endregion
  214. #region 获取指定目录中的文件列表
  215. /// <summary>
  216. /// 获取指定目录中所有文件列表
  217. /// </summary>
  218. /// <param name="directoryPath">指定目录的绝对路径</param>
  219. public static string[] GetFileNames(string directoryPath)
  220. {
  221. //如果目录不存在,则抛出异常
  222. if (!IsExistDirectory(directoryPath))
  223. {
  224. throw new FileNotFoundException();
  225. }
  226. //获取文件列表
  227. return Directory.GetFiles(directoryPath);
  228. }
  229. /// <summary>
  230. /// 获取指定目录及子目录中所有文件列表
  231. /// </summary>
  232. /// <param name="directoryPath">指定目录的绝对路径</param>
  233. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  234. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  235. /// <param name="isSearchChild">是否搜索子目录</param>
  236. public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
  237. {
  238. //如果目录不存在,则抛出异常
  239. if (!IsExistDirectory(directoryPath))
  240. {
  241. throw new FileNotFoundException();
  242. }
  243. try
  244. {
  245. return Directory.GetFiles(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  246. }
  247. catch
  248. {
  249. return null;
  250. }
  251. }
  252. #endregion
  253. #region 获取指定目录中的子目录列表
  254. /// <summary>
  255. /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
  256. /// </summary>
  257. /// <param name="directoryPath">指定目录的绝对路径</param>
  258. public static string[] GetDirectories(string directoryPath)
  259. {
  260. try
  261. {
  262. return Directory.GetDirectories(directoryPath);
  263. }
  264. catch
  265. {
  266. return null;
  267. }
  268. }
  269. /// <summary>
  270. /// 获取指定目录及子目录中所有子目录列表
  271. /// </summary>
  272. /// <param name="directoryPath">指定目录的绝对路径</param>
  273. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  274. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  275. /// <param name="isSearchChild">是否搜索子目录</param>
  276. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  277. {
  278. try
  279. {
  280. return Directory.GetDirectories(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  281. }
  282. catch
  283. {
  284. throw null;
  285. }
  286. }
  287. #endregion
  288. #region 向文本文件写入内容
  289. /// <summary>
  290. /// 向文本文件中写入内容
  291. /// </summary>
  292. /// <param name="filePath">文件的绝对路径</param>
  293. /// <param name="content">写入的内容</param>
  294. public static void WriteText(string filePath, string content)
  295. {
  296. //向文件写入内容
  297. File.WriteAllText(filePath, content);
  298. }
  299. #endregion
  300. #region 向文本文件的尾部追加内容
  301. /// <summary>
  302. /// 向文本文件的尾部追加内容
  303. /// </summary>
  304. /// <param name="filePath">文件的绝对路径</param>
  305. /// <param name="content">写入的内容</param>
  306. public static void AppendText(string filePath, string content)
  307. {
  308. File.AppendAllText(filePath, content);
  309. }
  310. #endregion
  311. #region 将现有文件的内容复制到新文件中
  312. /// <summary>
  313. /// 将源文件的内容复制到目标文件中
  314. /// </summary>
  315. /// <param name="sourceFilePath">源文件的绝对路径</param>
  316. /// <param name="destFilePath">目标文件的绝对路径</param>
  317. public static void Copy(string sourceFilePath, string destFilePath)
  318. {
  319. File.Copy(sourceFilePath, destFilePath, true);
  320. }
  321. #endregion
  322. #region 将文件移动到指定目录
  323. /// <summary>
  324. /// 将文件移动到指定目录
  325. /// </summary>
  326. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  327. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  328. public static void Move(string sourceFilePath, string descDirectoryPath)
  329. {
  330. //获取源文件的名称
  331. string sourceFileName = GetFileName(sourceFilePath);
  332. if (IsExistDirectory(descDirectoryPath))
  333. {
  334. //如果目标中存在同名文件,则删除
  335. if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
  336. {
  337. DeleteFile(descDirectoryPath + "\\" + sourceFileName);
  338. }
  339. //将文件移动到指定目录
  340. File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
  341. }
  342. }
  343. #endregion
  344. #region 将流读取到缓冲区中
  345. /// <summary>
  346. /// 将流读取到缓冲区中
  347. /// </summary>
  348. /// <param name="stream">原始流</param>
  349. public static byte[] StreamToBytes(Stream stream)
  350. {
  351. try
  352. {
  353. //创建缓冲区
  354. byte[] buffer = new byte[stream.Length];
  355. //读取流
  356. stream.Read(buffer, 0, int.Parse(stream.Length.ToString()));
  357. //返回流
  358. return buffer;
  359. }
  360. catch
  361. {
  362. return null;
  363. }
  364. finally
  365. {
  366. //关闭流
  367. stream.Close();
  368. }
  369. }
  370. #endregion
  371. #region 将文件读取到缓冲区中
  372. /// <summary>
  373. /// 将文件读取到缓冲区中
  374. /// </summary>
  375. /// <param name="filePath">文件的绝对路径</param>
  376. public static byte[] FileToBytes(string filePath)
  377. {
  378. //获取文件的大小
  379. int fileSize = GetFileSize(filePath);
  380. //创建一个临时缓冲区
  381. byte[] buffer = new byte[fileSize];
  382. //创建一个文件流
  383. FileInfo fi = new FileInfo(filePath);
  384. FileStream fs = fi.Open(FileMode.Open);
  385. try
  386. {
  387. //将文件流读入缓冲区
  388. fs.Read(buffer, 0, fileSize);
  389. return buffer;
  390. }
  391. catch
  392. {
  393. return null;
  394. }
  395. finally
  396. {
  397. //关闭文件流
  398. fs.Close();
  399. }
  400. }
  401. #endregion
  402. #region 将文件读取到字符串中
  403. /// <summary>
  404. /// 将文件读取到字符串中
  405. /// </summary>
  406. /// <param name="filePath">文件的绝对路径</param>
  407. public static string FileToString(string filePath)
  408. {
  409. return FileToString(filePath, Encoding.Default);
  410. }
  411. /// <summary>
  412. /// 将文件读取到字符串中
  413. /// </summary>
  414. /// <param name="filePath">文件的绝对路径</param>
  415. /// <param name="encoding">字符编码</param>
  416. public static string FileToString(string filePath, Encoding encoding)
  417. {
  418. //创建流读取器
  419. StreamReader reader = new StreamReader(filePath, encoding);
  420. try
  421. {
  422. //读取流
  423. return reader.ReadToEnd();
  424. }
  425. catch
  426. {
  427. return string.Empty;
  428. }
  429. finally
  430. {
  431. //关闭流读取器
  432. reader.Close();
  433. }
  434. }
  435. #endregion
  436. #region 从文件的绝对路径中获取文件名( 包含扩展名 )
  437. /// <summary>
  438. /// 从文件的绝对路径中获取文件名( 包含扩展名 )
  439. /// </summary>
  440. /// <param name="filePath">文件的绝对路径</param>
  441. public static string GetFileName(string filePath)
  442. {
  443. //获取文件的名称
  444. FileInfo fi = new FileInfo(filePath);
  445. return fi.Name;
  446. }
  447. #endregion
  448. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  449. /// <summary>
  450. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  451. /// </summary>
  452. /// <param name="filePath">文件的绝对路径</param>
  453. public static string GetFileNameNoExtension(string filePath)
  454. {
  455. //获取文件的名称
  456. FileInfo fi = new FileInfo(filePath);
  457. return fi.Name.Split('.')[0];
  458. }
  459. #endregion
  460. #region 从文件的绝对路径中获取扩展名
  461. /// <summary>
  462. /// 从文件的绝对路径中获取扩展名
  463. /// </summary>
  464. /// <param name="filePath">文件的绝对路径</param>
  465. public static string GetExtension(string filePath)
  466. {
  467. //获取文件的名称
  468. FileInfo fi = new FileInfo(filePath);
  469. return fi.Extension;
  470. }
  471. #endregion
  472. #region 清空指定目录
  473. /// <summary>
  474. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  475. /// </summary>
  476. /// <param name="directoryPath">指定目录的绝对路径</param>
  477. public static void ClearDirectory(string directoryPath)
  478. {
  479. if (IsExistDirectory(directoryPath))
  480. {
  481. //删除目录中所有的文件
  482. string[] fileNames = GetFileNames(directoryPath);
  483. foreach (string t in fileNames)
  484. {
  485. DeleteFile(t);
  486. }
  487. //删除目录中所有的子目录
  488. string[] directoryNames = GetDirectories(directoryPath);
  489. foreach (string t in directoryNames)
  490. {
  491. DeleteDirectory(t);
  492. }
  493. }
  494. }
  495. #endregion
  496. #region 清空文件内容
  497. /// <summary>
  498. /// 清空文件内容
  499. /// </summary>
  500. /// <param name="filePath">文件的绝对路径</param>
  501. public static void ClearFile(string filePath)
  502. {
  503. //删除文件
  504. File.Delete(filePath);
  505. //重新创建该文件
  506. CreateFile(filePath);
  507. }
  508. #endregion
  509. #region 删除指定文件
  510. /// <summary>
  511. /// 删除指定文件
  512. /// </summary>
  513. /// <param name="filePath">文件的绝对路径</param>
  514. public static void DeleteFile(string filePath)
  515. {
  516. if (IsExistFile(filePath))
  517. {
  518. File.Delete(filePath);
  519. }
  520. }
  521. #endregion
  522. #region 删除指定目录
  523. /// <summary>
  524. /// 删除指定目录及其所有子目录
  525. /// </summary>
  526. /// <param name="directoryPath">指定目录的绝对路径</param>
  527. public static void DeleteDirectory(string directoryPath)
  528. {
  529. if (IsExistDirectory(directoryPath))
  530. {
  531. Directory.Delete(directoryPath, true);
  532. }
  533. }
  534. #endregion
  535. #region 记录错误日志到文件方法
  536. /// <summary>
  537. /// 记录错误日志到文件方法
  538. /// </summary>
  539. /// <param name="exMessage"></param>
  540. /// <param name="exMethod"></param>
  541. /// <param name="userID"></param>
  542. public static void ErrorLog(string exMessage, string exMethod, int userID)
  543. {
  544. try
  545. {
  546. string errVir = "/Log/Error/" + DateTime.Now.ToShortDateString() + ".txt";
  547. string errPath = System.Web.HttpContext.Current.Server.MapPath(errVir);
  548. File.AppendAllText(errPath,
  549. "{userID:" + userID + ",exMedthod:" + exMethod + ",exMessage:" + exMessage + "}");
  550. }
  551. catch
  552. {
  553. }
  554. }
  555. #endregion
  556. #region 输出调试日志
  557. /// <summary>
  558. /// 输出调试日志
  559. /// </summary>
  560. /// <param name="factValue">实际值</param>
  561. /// <param name="expectValue">预期值</param>
  562. public static void OutDebugLog(object factValue, object expectValue = null)
  563. {
  564. string errPath = System.Web.HttpContext.Current.Server.MapPath(string.Format("/Log/Debug/{0}.html", DateTime.Now.ToShortDateString()));
  565. if (!Equals(expectValue, null))
  566. File.AppendAllLines(errPath,
  567. new[]{string.Format(
  568. "【{0}】[{3}] 实际值:<span style='color:blue;'>{1}</span> 预期值: <span style='color:gray;'>{2}</span><br/>",
  569. DateTime.Now.ToShortTimeString()
  570. , factValue, expectValue, Equals(expectValue, factValue)
  571. ? "<span style='color:green;'>成功</span>"
  572. : "<span style='color:red;'>失败</span>")});
  573. else
  574. File.AppendAllLines(errPath, new[]{
  575. string.Format(
  576. "【{0}】[{3}] 实际值:<span style='color:blue;'>{1}</span> 预期值: <span style='color:gray;'>{2}</span><br/>",
  577. DateTime.Now.ToShortTimeString()
  578. , factValue, "空", "<span style='color:green;'>成功</span>")});
  579. }
  580. #endregion
  581. }
  582. }

最新文章

  1. [转]android:动态创建多个按钮 及 批量设置监听
  2. 求最长回文子串:Manacher算法
  3. IOS-JSON &amp; XML解析
  4. window.open打开窗体和if嵌套
  5. linux kill信号列表
  6. 1.Nexus安装与配置
  7. (转帖)BootStrap入门教程 (三)
  8. BootStrap2学习日记2--将固定布局换成响应式布局
  9. MSSQLSERVER- CharIndex的妙用,找出有妙用
  10. linux 添加 $path
  11. 关于JS历史拓展
  12. ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)
  13. Unity编程标准导引-3.3 Transform
  14. win7下elasticsearch5.0 安装head插件
  15. CentOS 7安装squid代理服务器
  16. dart 如何优雅的避空
  17. 如何查看卖家ID (Merchant ID) 亚马逊哪里找?
  18. python中类方法,实例方法,静态方法的作用和区别
  19. [转]sqlserver转换为Mysql工具使用
  20. PHP session用redis存储

热门文章

  1. Chromuim proxy Api 提取代里proxy调用Chrome隐身多窗口 多COOKIE 工具
  2. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
  3. sql中的跨库查询
  4. js 防止页面后退的方法
  5. UIScrollView的使用
  6. C++中vector的remove用法
  7. webstorm 运行配置gulp
  8. MySQL binlog的格式解析
  9. JSON转Dictionary
  10. FLASH结构