18位长度的计时周期数: DateTime.Now.Ticks.ToString()
多数是收集而来,加上测试感觉很不错,分享一下或许有些帮助吧:
引用:
- using System;
- using System.Text;
- using System.IO;
主代码:
- namespace PorjectTools
- {
-
-
- public static class FileHelper
- {
- #region 检测指定目录是否存在
-
-
-
-
- public static bool IsExistDirectory(string directoryPath)
- {
- return Directory.Exists(directoryPath);
- }
- #endregion
-
- #region 检测指定文件是否存在
-
-
-
-
- public static bool IsExistFile(string filePath)
- {
- return File.Exists(filePath);
- }
- #endregion
-
- #region 检测指定目录是否为空
-
-
-
-
- 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 检测指定目录中是否存在指定的文件
-
-
-
-
-
-
- public static bool Contains(string directoryPath, string searchPattern)
- {
- try
- {
-
- string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
-
-
- return fileNames.Length != 0;
- }
- catch
- {
- return false;
- }
- }
-
-
-
-
-
-
-
-
- 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 创建一个目录
-
-
-
-
- public static void CreateDirectory(string directoryPath)
- {
-
- if (!IsExistDirectory(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- }
- #endregion
-
- #region 创建一个文件
-
-
-
-
- public static bool CreateFile(string filePath)
- {
- try
- {
-
- if (!IsExistFile(filePath))
- {
-
- FileInfo file = new FileInfo(filePath);
-
- FileStream fs = file.Create();
-
- fs.Close();
- }
- }
- catch
- {
- return false;
- }
-
- return true;
- }
-
-
-
-
-
-
- public static bool CreateFile(string filePath, byte[] buffer)
- {
- try
- {
-
- if (!IsExistFile(filePath))
- {
-
- FileInfo file = new FileInfo(filePath);
-
-
- FileStream fs = file.Create();
-
-
- fs.Write(buffer, 0, buffer.Length);
-
-
- fs.Close();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- #endregion
-
- #region 获取文本文件的行数
-
-
-
-
- public static int GetLineCount(string filePath)
- {
-
- string[] rows = File.ReadAllLines(filePath);
-
-
- return rows.Length;
- }
- #endregion
-
- #region 获取一个文件的长度
-
-
-
-
- public static int GetFileSize(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
-
-
- return (int)fi.Length;
- }
-
-
-
-
-
- public static double GetFileSizeByKB(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
- long size = fi.Length / 1024;
-
- return double.Parse(size.ToString());
- }
-
-
-
-
-
- public static double GetFileSizeByMB(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
- long size = fi.Length / 1024 / 1024;
-
- return double.Parse(size.ToString());
- }
- #endregion
-
- #region 获取指定目录中的文件列表
-
-
-
-
- public static string[] GetFileNames(string directoryPath)
- {
-
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
-
-
- return Directory.GetFiles(directoryPath);
- }
-
-
-
-
-
-
-
-
- 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 获取指定目录中的子目录列表
-
-
-
-
- public static string[] GetDirectories(string directoryPath)
- {
- try
- {
- return Directory.GetDirectories(directoryPath);
- }
- catch
- {
- return null;
- }
- }
-
-
-
-
-
-
-
-
- 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 向文本文件写入内容
-
-
-
-
-
- public static void WriteText(string filePath, string content)
- {
-
- File.WriteAllText(filePath, content);
- }
- #endregion
-
- #region 向文本文件的尾部追加内容
-
-
-
-
-
- public static void AppendText(string filePath, string content)
- {
- File.AppendAllText(filePath, content);
- }
- #endregion
-
- #region 将现有文件的内容复制到新文件中
-
-
-
-
-
- public static void Copy(string sourceFilePath, string destFilePath)
- {
- File.Copy(sourceFilePath, destFilePath, true);
- }
- #endregion
-
- #region 将文件移动到指定目录
-
-
-
-
-
- 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 将流读取到缓冲区中
-
-
-
-
- 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 将文件读取到缓冲区中
-
-
-
-
- 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 将文件读取到字符串中
-
-
-
-
- public static string FileToString(string filePath)
- {
- return FileToString(filePath, Encoding.Default);
- }
-
-
-
-
-
- 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 从文件的绝对路径中获取文件名( 包含扩展名 )
-
-
-
-
- public static string GetFileName(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
- return fi.Name;
- }
- #endregion
-
- #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
-
-
-
-
- public static string GetFileNameNoExtension(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
- return fi.Name.Split(‘.‘)[0];
- }
- #endregion
-
- #region 从文件的绝对路径中获取扩展名
-
-
-
-
- public static string GetExtension(string filePath)
- {
-
- FileInfo fi = new FileInfo(filePath);
- return fi.Extension;
- }
- #endregion
-
- #region 清空指定目录
-
-
-
-
- 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 清空文件内容
-
-
-
-
- public static void ClearFile(string filePath)
- {
-
- File.Delete(filePath);
-
-
- CreateFile(filePath);
- }
- #endregion
-
- #region 删除指定文件
-
-
-
-
- public static void DeleteFile(string filePath)
- {
- if (IsExistFile(filePath))
- {
- File.Delete(filePath);
- }
- }
- #endregion
-
- #region 删除指定目录
-
-
-
-
- public static void DeleteDirectory(string directoryPath)
- {
- if (IsExistDirectory(directoryPath))
- {
- Directory.Delete(directoryPath, true);
- }
- }
- #endregion
-
- #region 记录错误日志到文件方法
-
-
-
-
-
-
- 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 输出调试日志
-
-
-
-
-
- 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
- }
- }
c# 文件及目录操作类
原文:http://www.cnblogs.com/host-2008/p/5937799.html