惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 夜来风雨香

【整理】C#文件操作大全(SamWang)<转> BIOS设置之UEFI/Legacy BIOS切换图文详解 UEFI+GPT引导实践篇(一):切换到UEFI启动,准备安装介质 UEFI+GPT引导实践篇(二):UEFI引导安装64位Win7/Win8 UEFI+GPT引导基础篇(一):什么是GPT,什么是UEFI? 预装WIN8系统的电脑安装WIN7的方法 串口 COM口 TTL RS-232 RS-485 区别 释疑 DataGridView导出到Excel的三个方法 WinForm DataGridView分页功能 C#GridViewExport帮助类,美化导出 C#EXCEL 操作类--C#DataToExcel帮助类 C#EXCEL 操作类--C#ExcelHelper操作类 VS2010 项目引用了DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称 <转> FPGA Verilog HDL 系列实例--------步进电机驱动控制 C++使用VARIANT实现二维数组的操作 VS2010+VMWare8+VisualDDK1.5.6 创建并调试你的第一个驱动程序 - 完全教程 USB编程研究之二(常见设备类型的GUID) C++——CString用法大全 C++ 如何有效地使用对话框
c# 文件及目录操作类
夜来风雨香 · 2016-10-08 · via 博客园 - 夜来风雨香

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

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

引用:

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


主代码:

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