18位長度的計時周期數: DateTime.Now.Ticks.ToString()
多數是收集而來,加上測試感覺很不錯,分享一下或許有些幫助吧:
引用:
[csharp]
view plain copy
- using System;
- using System.Text;
- using System.IO;
主代碼:
[csharp]
view plain copy
- namespace PorjectTools
- {
- ///<summary>
- ///</summary>
- public static class FileHelper
- {
- #region 檢測指定目錄是否存在
- /// <summary>
- /// 檢測指定目錄是否存在
- /// </summary>
- /// <param name="directoryPath">目錄的絕對路徑</param>
- public static bool IsExistDirectory(string directoryPath)
- {
- return Directory.Exists(directoryPath);
- }
- #endregion
- #region 檢測指定文件是否存在
- /// <summary>
- /// 檢測指定文件是否存在,如果存在則返回true。
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static bool IsExistFile(string filePath)
- {
- return File.Exists(filePath);
- }
- #endregion
- #region 檢測指定目錄是否為空
- /// <summary>
- /// 檢測指定目錄是否為空
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- public static bool IsEmptyDirectory(string directoryPath)
- {
- try
- {
- //判斷是否存在文件
- string[] fileNames = GetFileNames(directoryPath);
- if (fileNames.Length > 0)
- {
- return false;
- }
- //判斷是否存在文件夾
- string[] directoryNames = GetDirectories(directoryPath);
- return directoryNames.Length <= 0;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 檢測指定目錄中是否存在指定的文件
- /// <summary>
- /// 檢測指定目錄中是否存在指定的文件,若要搜索子目錄請使用重載方法.
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
- /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>
- public static bool Contains(string directoryPath, string searchPattern)
- {
- try
- {
- //獲取指定的文件列表
- string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
- //判斷指定文件是否存在
- return fileNames.Length != 0;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 檢測指定目錄中是否存在指定的文件
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
- /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目錄</param>
- public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
- {
- try
- {
- //獲取指定的文件列表
- string[] fileNames = GetFileNames(directoryPath, searchPattern, true);
- //判斷指定文件是否存在
- return fileNames.Length != 0;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 創建一個目錄
- /// <summary>
- /// 創建一個目錄
- /// </summary>
- /// <param name="directoryPath">目錄的絕對路徑</param>
- public static void CreateDirectory(string directoryPath)
- {
- //如果目錄不存在則創建該目錄
- if (!IsExistDirectory(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- }
- #endregion
- #region 創建一個文件
- /// <summary>
- /// 創建一個文件。
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static bool CreateFile(string filePath)
- {
- try
- {
- //如果文件不存在則創建該文件
- if (!IsExistFile(filePath))
- {
- //創建一個FileInfo對象
- FileInfo file = new FileInfo(filePath);
- //創建文件
- FileStream fs = file.Create();
- //關閉文件流
- fs.Close();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 創建一個文件,並將字節流寫入文件。
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- /// <param name="buffer">二進制流數據</param>
- public static bool CreateFile(string filePath, byte[] buffer)
- {
- try
- {
- //如果文件不存在則創建該文件
- if (!IsExistFile(filePath))
- {
- //創建一個FileInfo對象
- FileInfo file = new FileInfo(filePath);
- //創建文件
- FileStream fs = file.Create();
- //寫入二進制流
- fs.Write(buffer, 0, buffer.Length);
- //關閉文件流
- fs.Close();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- #endregion
- #region 獲取文本文件的行數
- /// <summary>
- /// 獲取文本文件的行數
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static int GetLineCount(string filePath)
- {
- //將文本文件的各行讀到一個字符串數組中
- string[] rows = File.ReadAllLines(filePath);
- //返回行數
- return rows.Length;
- }
- #endregion
- #region 獲取一個文件的長度
- /// <summary>
- /// 獲取一個文件的長度,單位為Byte
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static int GetFileSize(string filePath)
- {
- //創建一個文件對象
- FileInfo fi = new FileInfo(filePath);
- //獲取文件的大小
- return (int)fi.Length;
- }
- /// <summary>
- /// 獲取一個文件的長度,單位為KB
- /// </summary>
- /// <param name="filePath">文件的路徑</param>
- public static double GetFileSizeByKB(string filePath)
- {
- //創建一個文件對象
- FileInfo fi = new FileInfo(filePath);
- long size = fi.Length / 1024;
- //獲取文件的大小
- return double.Parse(size.ToString());
- }
- /// <summary>
- /// 獲取一個文件的長度,單位為MB
- /// </summary>
- /// <param name="filePath">文件的路徑</param>
- public static double GetFileSizeByMB(string filePath)
- {
- //創建一個文件對象
- FileInfo fi = new FileInfo(filePath);
- long size = fi.Length / 1024 / 1024;
- //獲取文件的大小
- return double.Parse(size.ToString());
- }
- #endregion
- #region 獲取指定目錄中的文件列表
- /// <summary>
- /// 獲取指定目錄中所有文件列表
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- public static string[] GetFileNames(string directoryPath)
- {
- //如果目錄不存在,則拋出異常
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
- //獲取文件列表
- return Directory.GetFiles(directoryPath);
- }
- /// <summary>
- /// 獲取指定目錄及子目錄中所有文件列表
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
- /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目錄</param>
- public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
- {
- //如果目錄不存在,則拋出異常
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
- try
- {
- return Directory.GetFiles(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
- }
- catch
- {
- return null;
- }
- }
- #endregion
- #region 獲取指定目錄中的子目錄列表
- /// <summary>
- /// 獲取指定目錄中所有子目錄列表,若要搜索嵌套的子目錄列表,請使用重載方法.
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- public static string[] GetDirectories(string directoryPath)
- {
- try
- {
- return Directory.GetDirectories(directoryPath);
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 獲取指定目錄及子目錄中所有子目錄列表
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N個字符,"?"代表1個字符。
- /// 范例:"Log*.xml"表示搜索所有以Log開頭的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目錄</param>
- public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
- {
- try
- {
- return Directory.GetDirectories(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
- }
- catch
- {
- throw null;
- }
- }
- #endregion
- #region 向文本文件寫入內容
- /// <summary>
- /// 向文本文件中寫入內容
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- /// <param name="content">寫入的內容</param>
- public static void WriteText(string filePath, string content)
- {
- //向文件寫入內容
- File.WriteAllText(filePath, content);
- }
- #endregion
- #region 向文本文件的尾部追加內容
- /// <summary>
- /// 向文本文件的尾部追加內容
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- /// <param name="content">寫入的內容</param>
- public static void AppendText(string filePath, string content)
- {
- File.AppendAllText(filePath, content);
- }
- #endregion
- #region 將現有文件的內容復制到新文件中
- /// <summary>
- /// 將源文件的內容復制到目標文件中
- /// </summary>
- /// <param name="sourceFilePath">源文件的絕對路徑</param>
- /// <param name="destFilePath">目標文件的絕對路徑</param>
- public static void Copy(string sourceFilePath, string destFilePath)
- {
- File.Copy(sourceFilePath, destFilePath, true);
- }
- #endregion
- #region 將文件移動到指定目錄
- /// <summary>
- /// 將文件移動到指定目錄
- /// </summary>
- /// <param name="sourceFilePath">需要移動的源文件的絕對路徑</param>
- /// <param name="descDirectoryPath">移動到的目錄的絕對路徑</param>
- public static void Move(string sourceFilePath, string descDirectoryPath)
- {
- //獲取源文件的名稱
- string sourceFileName = GetFileName(sourceFilePath);
- if (IsExistDirectory(descDirectoryPath))
- {
- //如果目標中存在同名文件,則刪除
- if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
- {
- DeleteFile(descDirectoryPath + "\\" + sourceFileName);
- }
- //將文件移動到指定目錄
- File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
- }
- }
- #endregion
- #region 將流讀取到緩沖區中
- /// <summary>
- /// 將流讀取到緩沖區中
- /// </summary>
- /// <param name="stream">原始流</param>
- public static byte[] StreamToBytes(Stream stream)
- {
- try
- {
- //創建緩沖區
- byte[] buffer = new byte[stream.Length];
- //讀取流
- stream.Read(buffer, 0, int.Parse(stream.Length.ToString()));
- //返回流
- return buffer;
- }
- catch
- {
- return null;
- }
- finally
- {
- //關閉流
- stream.Close();
- }
- }
- #endregion
- #region 將文件讀取到緩沖區中
- /// <summary>
- /// 將文件讀取到緩沖區中
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static byte[] FileToBytes(string filePath)
- {
- //獲取文件的大小
- int fileSize = GetFileSize(filePath);
- //創建一個臨時緩沖區
- byte[] buffer = new byte[fileSize];
- //創建一個文件流
- FileInfo fi = new FileInfo(filePath);
- FileStream fs = fi.Open(FileMode.Open);
- try
- {
- //將文件流讀入緩沖區
- fs.Read(buffer, 0, fileSize);
- return buffer;
- }
- catch
- {
- return null;
- }
- finally
- {
- //關閉文件流
- fs.Close();
- }
- }
- #endregion
- #region 將文件讀取到字符串中
- /// <summary>
- /// 將文件讀取到字符串中
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static string FileToString(string filePath)
- {
- return FileToString(filePath, Encoding.Default);
- }
- /// <summary>
- /// 將文件讀取到字符串中
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- /// <param name="encoding">字符編碼</param>
- public static string FileToString(string filePath, Encoding encoding)
- {
- //創建流讀取器
- StreamReader reader = new StreamReader(filePath, encoding);
- try
- {
- //讀取流
- return reader.ReadToEnd();
- }
- catch
- {
- return string.Empty;
- }
- finally
- {
- //關閉流讀取器
- reader.Close();
- }
- }
- #endregion
- #region 從文件的絕對路徑中獲取文件名( 包含擴展名 )
- /// <summary>
- /// 從文件的絕對路徑中獲取文件名( 包含擴展名 )
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static string GetFileName(string filePath)
- {
- //獲取文件的名稱
- FileInfo fi = new FileInfo(filePath);
- return fi.Name;
- }
- #endregion
- #region 從文件的絕對路徑中獲取文件名( 不包含擴展名 )
- /// <summary>
- /// 從文件的絕對路徑中獲取文件名( 不包含擴展名 )
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static string GetFileNameNoExtension(string filePath)
- {
- //獲取文件的名稱
- FileInfo fi = new FileInfo(filePath);
- return fi.Name.Split('.')[0];
- }
- #endregion
- #region 從文件的絕對路徑中獲取擴展名
- /// <summary>
- /// 從文件的絕對路徑中獲取擴展名
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static string GetExtension(string filePath)
- {
- //獲取文件的名稱
- FileInfo fi = new FileInfo(filePath);
- return fi.Extension;
- }
- #endregion
- #region 清空指定目錄
- /// <summary>
- /// 清空指定目錄下所有文件及子目錄,但該目錄依然保存.
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- public static void ClearDirectory(string directoryPath)
- {
- if (IsExistDirectory(directoryPath))
- {
- //刪除目錄中所有的文件
- string[] fileNames = GetFileNames(directoryPath);
- foreach (string t in fileNames)
- {
- DeleteFile(t);
- }
- //刪除目錄中所有的子目錄
- string[] directoryNames = GetDirectories(directoryPath);
- foreach (string t in directoryNames)
- {
- DeleteDirectory(t);
- }
- }
- }
- #endregion
- #region 清空文件內容
- /// <summary>
- /// 清空文件內容
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static void ClearFile(string filePath)
- {
- //刪除文件
- File.Delete(filePath);
- //重新創建該文件
- CreateFile(filePath);
- }
- #endregion
- #region 刪除指定文件
- /// <summary>
- /// 刪除指定文件
- /// </summary>
- /// <param name="filePath">文件的絕對路徑</param>
- public static void DeleteFile(string filePath)
- {
- if (IsExistFile(filePath))
- {
- File.Delete(filePath);
- }
- }
- #endregion
- #region 刪除指定目錄
- /// <summary>
- /// 刪除指定目錄及其所有子目錄
- /// </summary>
- /// <param name="directoryPath">指定目錄的絕對路徑</param>
- public static void DeleteDirectory(string directoryPath)
- {
- if (IsExistDirectory(directoryPath))
- {
- Directory.Delete(directoryPath, true);
- }
- }
- #endregion
- #region 記錄錯誤日志到文件方法
- /// <summary>
- /// 記錄錯誤日志到文件方法
- /// </summary>
- /// <param name="exMessage"></param>
- /// <param name="exMethod"></param>
- /// <param name="userID"></param>
- public static void ErrorLog(string exMessage, string exMethod, int userID)
- {
- try
- {
- string errVir = "/Log/Error/" + DateTime.Now.ToShortDateString() + ".txt";
- string errPath = System.Web.HttpContext.Current.Server.MapPath(errVir);
- File.AppendAllText(errPath,
- "{userID:" + userID + ",exMedthod:" + exMethod + ",exMessage:" + exMessage + "}");
- }
- catch
- {
- }
- }
- #endregion
- #region 輸出調試日志
- /// <summary>
- /// 輸出調試日志
- /// </summary>
- /// <param name="factValue">實際值</param>
- /// <param name="expectValue">預期值</param>
- public static void OutDebugLog(object factValue, object expectValue = null)
- {
- string errPath = System.Web.HttpContext.Current.Server.MapPath(string.Format("/Log/Debug/{0}.html", DateTime.Now.ToShortDateString()));
- if (!Equals(expectValue, null))
- File.AppendAllLines(errPath,
- new[]{string.Format(
- "【{0}】[{3}] 實際值:<span style='color:blue;'>{1}</span> 預期值: <span style='color:gray;'>{2}</span><br/>",
- DateTime.Now.ToShortTimeString()
- , factValue, expectValue, Equals(expectValue, factValue)
- ? "<span style='color:green;'>成功</span>"
- : "<span style='color:red;'>失敗</span>")});
- else
- File.AppendAllLines(errPath, new[]{
- string.Format(
- "【{0}】[{3}] 實際值:<span style='color:blue;'>{1}</span> 預期值: <span style='color:gray;'>{2}</span><br/>",
- DateTime.Now.ToShortTimeString()
- , factValue, "空", "<span style='color:green;'>成功</span>")});
- }
- #endregion
- }
- }