|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace Common.system
- {
- /// <summary>
- /// 文件工具公共方法
- /// 创建人:赵耀
- /// 创建时间:2018年11月6日
- /// </summary>
- public static class FileToolsCommon
- {
- /// <summary>
- /// 获取文件的绝对路径
- /// </summary>
- /// <param name="Path">相对路径</param>
- /// <returns></returns>
- public static string GetFileAbsolutePath(string Path="")
- {
- try
- {
- if (!string.IsNullOrWhiteSpace(Path))
- {
- Path = Path.Replace("\\", "/");
- if (Path != "/")
- {
- if (Path.Substring(1, 1) == ":")
- return Path;
- if (Path.Substring(0, 1) != "/")
- Path = "/" + Path;
- }
- }
- string AbsolutePath = Application.StartupPath.ToString().Replace("\\", "/") + Path;
- return AbsolutePath;
- }
- catch (Exception)
- {
- return Path;
- }
- }
-
- #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)
- {
- try
- {
- //如果目录不存在则创建该目录
- if (!IsExistDirectory(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- }
- catch (Exception)
- {
- throw new ApplicationException("请使用管理员权限运行!");
- }
- }
- #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 FileInfo[] GetFileInfos(string directoryPath,string files=null)
- {
- //如果目录不存在,则抛出异常
- if (!IsExistDirectory(directoryPath))
- {
- return null;//throw new FileNotFoundException();
- }
- DirectoryInfo folder = new DirectoryInfo(directoryPath);
- FileInfo[] fileList = null;
- if (files == null)
- fileList = folder.GetFiles();
- else
- fileList = folder.GetFiles(files);
- //获取文件列表
- return fileList;
- }
-
- /// <summary>
- /// 获取指定目录中所有文件列表
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- public static string[] GetFileNames(string directoryPath)
- {
- //如果目录不存在,则抛出异常
- if (!IsExistDirectory(directoryPath))
- {
- return null;//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))
- {
- return null;//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
- {
- return null;//throw
- }
- }
- #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);
- return FileToString(filePath, Encoding.UTF8);
- }
- /// <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="directoryPath">目录地址</param>
- /// <returns> 单位:MB</returns>
- public static double pathSize(string directoryPath)
- {
- DirectoryInfo folder = new DirectoryInfo(directoryPath);
- double DirectorySize = DirSize(folder);
- for (int i = 0; i < 2; i++)
- {
- DirectorySize /= (double)1024;
- }
- return DirectorySize;
- }
-
- /// <summary>
- /// 获取目录大小
- /// </summary>
- /// <param name="d">目录</param>
- /// <returns>字节</returns>
- public static long DirSize(DirectoryInfo d)
- {
- long Size = 0;
- // 所有文件大小.
- FileInfo[] fis = d.GetFiles();
- foreach (FileInfo fi in fis)
- {
- Size += fi.Length;
- }
- // 遍历出当前目录的所有文件夹.
- DirectoryInfo[] dis = d.GetDirectories();
- foreach (DirectoryInfo di in dis)
- {
- Size += DirSize(di); //这就用到递归了,调用父方法,注意,这里并不是直接返回值,而是调用父返回来的
- }
- return (Size);
- }
- #endregion
-
- #region 移动目录
- /// <summary>
- /// 移动目录并重命名
- /// </summary>
- /// <param name="directoryPath">操作目录绝对路径</param>
- /// <param name="newDirectoryPath">目标目录绝对路径</param>
- /// <param name="Message">返回错误消息,成功则为空</param>
- /// <returns></returns>
- public static bool MoveDirectory(string directoryPath, string newDirectoryPath, out string Message)
- {
- Message = "";
- try
- {
- if (!IsExistDirectory(directoryPath))//判断目录是否存在
- {
- Message = "操作目录不存在!";
- return false;
- }
- if (IsExistDirectory(newDirectoryPath))
- {
- Message = "目标目录已存在!";
- return false;
- }
- Directory.Move(directoryPath, newDirectoryPath);
- return true;
- }
- catch (Exception ex)
- {
- Message = ex.Message;
- return false;
- }
- }
- #endregion
-
- #region 操作配置文件
- #region 从配置文件获取Value
- /// <summary>
- /// 从配置文件获取Value
- /// </summary>
- /// <param name="key">配置文件中key字符串</param>
- /// <returns></returns>
- public static string GetConfigValue(string key)
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- //获取AppSettings的节点
- AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
- return appsection.Settings[key].Value;
- }
- catch
- {
- return "";
- }
- }
- #endregion
-
- #region 设置配置文件
- /// <summary>
- /// 设置配置文件
- /// </summary>
- /// <param name="key">配置文件中key字符串</param>
- /// <param name="value">配置文件中value字符串</param>
- /// <returns></returns>
- public static bool SetConfigValue(string key, string value)
- {
- try
- {
- //打开配置文件
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- //获取AppSettings的节点
- AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
- appsection.Settings[key].Value = value;
- config.Save();
-
- return true;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #endregion
- }
- }
|