using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Xml; namespace WinformGeneralDeveloperFrame.Commons { /// /// 常用的文件操作辅助类FileUtil /// public class FileUtil { #region Stream、byte[] 和 文件之间的转换 /// /// 将流读取到缓冲区中 /// /// 原始流 public static byte[] StreamToBytes(Stream stream) { try { //创建缓冲区 byte[] buffer = new byte[stream.Length]; //读取流 stream.Read(buffer, 0, Convert.ToInt32(stream.Length)); //返回流 return buffer; } catch (IOException ex) { throw ex; } finally { //关闭流 stream.Close(); } } /// /// 将 byte[] 转成 Stream /// public static Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; } /// /// 将 Stream 写入文件 /// public static void StreamToFile(Stream stream, string fileName) { // 把 Stream 转换成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); // 把 byte[] 写入文件 FileStream fs = new FileStream(fileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); } /// /// 从文件读取 Stream /// public static Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); return stream; } /// /// 将文件读取到缓冲区中 /// /// 文件的绝对路径 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 (IOException ex) { throw ex; } finally { //关闭文件流 fs.Close(); } } /// /// 将文件读取到字符串中 /// /// 文件的绝对路径 public static string FileToString(string filePath) { return FileToString(filePath, Encoding.Default); } /// /// 将文件读取到字符串中 /// /// 文件的绝对路径 /// 字符编码 public static string FileToString(string filePath, Encoding encoding) { try { //创建流读取器 using (StreamReader reader = new StreamReader(filePath, encoding)) { //读取流 return reader.ReadToEnd(); } } catch (IOException ex) { throw ex; } } /// /// 从嵌入资源中读取文件内容(e.g: xml). /// /// 嵌入资源文件名,包括项目的命名空间. /// 资源中的文件内容. public static string ReadFileFromEmbedded(string fileWholeName) { string result = string.Empty; using (TextReader reader = new StreamReader( Assembly.GetExecutingAssembly().GetManifestResourceStream(fileWholeName))) { result = reader.ReadToEnd(); } return result; } #endregion #region 获取文件的编码类型 /// /// 获取文件编码 /// /// 文件绝对路径 /// public static Encoding GetEncoding(string filePath) { return GetEncoding(filePath, Encoding.Default); } /// /// 获取文件编码 /// /// 文件绝对路径 /// 找不到则返回这个默认编码 /// public static Encoding GetEncoding(string filePath, Encoding defaultEncoding) { Encoding targetEncoding = defaultEncoding; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4)) { if (fs != null && fs.Length >= 2) { long pos = fs.Position; fs.Position = 0; int[] buffer = new int[4]; //long x = fs.Seek(0, SeekOrigin.Begin); //fs.Read(buffer,0,4); buffer[0] = fs.ReadByte(); buffer[1] = fs.ReadByte(); buffer[2] = fs.ReadByte(); buffer[3] = fs.ReadByte(); fs.Position = pos; if (buffer[0] == 0xFE && buffer[1] == 0xFF) //UnicodeBe { targetEncoding = Encoding.BigEndianUnicode; } if (buffer[0] == 0xFF && buffer[1] == 0xFE) //Unicode { targetEncoding = Encoding.Unicode; } if (buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF) //UTF8 { targetEncoding = Encoding.UTF8; } } } return targetEncoding; } #endregion #region 文件操作 #region 获取一个文件的长度 /// /// 获取一个文件的长度,单位为Byte /// /// 文件的绝对路径 public static int GetFileSize(string filePath) { //创建一个文件对象 FileInfo fi = new FileInfo(filePath); //获取文件的大小 return (int) fi.Length; } /// /// 获取一个文件的长度,单位为KB /// /// 文件的路径 public static double GetFileSizeKB(string filePath) { //创建一个文件对象 FileInfo fi = new FileInfo(filePath); //获取文件的大小 return ConvertHelper.ToDouble(Convert.ToDouble(fi.Length) / 1024, 1); } /// /// 获取一个文件的长度,单位为MB /// /// 文件的路径 public static double GetFileSizeMB(string filePath) { //创建一个文件对象 FileInfo fi = new FileInfo(filePath); //获取文件的大小 return ConvertHelper.ToDouble(Convert.ToDouble(fi.Length) / 1024 / 1024, 1); } #endregion /// /// 向文本文件中写入内容 /// /// 文件的绝对路径 /// 写入的内容 public static void WriteText(string filePath, string content) { //向文件写入内容 File.WriteAllText(filePath, content, Encoding.Default); } /// /// 向文本文件的尾部追加内容 /// /// 文件的绝对路径 /// 写入的内容 public static void AppendText(string filePath, string content) { File.AppendAllText(filePath, content, Encoding.Default); } /// /// 将源文件的内容复制到目标文件中 /// /// 源文件的绝对路径 /// 目标文件的绝对路径 public static void Copy(string sourceFilePath, string destFilePath) { File.Copy(sourceFilePath, destFilePath, true); } /// /// 将文件移动到指定目录 /// /// 需要移动的源文件的绝对路径 /// 移动到的目录的绝对路径 public static void Move(string sourceFilePath, string descDirectoryPath) { //获取源文件的名称 string sourceFileName = GetFileName(sourceFilePath); if (Directory.Exists(descDirectoryPath)) { //如果目标中存在同名文件,则删除 if (IsExistFile(descDirectoryPath + "\\" + sourceFileName)) { DeleteFile(descDirectoryPath + "\\" + sourceFileName); } //将文件移动到指定目录 File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName); } } /// /// 检测指定文件是否存在,如果存在则返回true。 /// /// 文件的绝对路径 public static bool IsExistFile(string filePath) { return File.Exists(filePath); } /// /// 创建一个文件。 /// /// 文件的绝对路径 public static void CreateFile(string filePath) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { File.Create(filePath); } } catch (IOException ex) { throw ex; } } /// /// 创建一个文件,并将字节流写入文件。 /// /// 文件的绝对路径 /// 二进制流数据 public static void CreateFile(string filePath, byte[] buffer) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { //创建文件 using (FileStream fs = File.Create(filePath)) { //写入二进制流 fs.Write(buffer, 0, buffer.Length); } } } catch (IOException ex) { throw ex; } } /// /// 获取文本文件的行数 /// /// 文件的绝对路径 public static int GetLineCount(string filePath) { //将文本文件的各行读到一个字符串数组中 string[] rows = File.ReadAllLines(filePath); //返回行数 return rows.Length; } /// /// 从文件的绝对路径中获取文件名( 包含扩展名 ) /// /// 文件的绝对路径 public static string GetFileName(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name; } /// /// 从文件的绝对路径中获取文件名( 不包含扩展名 ) /// /// 文件的绝对路径 public static string GetFileNameNoExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name.Substring(0, fi.Name.LastIndexOf('.')); } /// /// 从文件的绝对路径中获取扩展名 /// /// 文件的绝对路径 public static string GetExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Extension; } /// /// 清空文件内容 /// /// 文件的绝对路径 public static void ClearFile(string filePath) { //删除文件 File.Delete(filePath); //重新创建该文件 CreateFile(filePath); } /// /// 删除指定文件 /// /// 文件的绝对路径 public static void DeleteFile(string filePath) { if (IsExistFile(filePath)) { File.Delete(filePath); } } /// /// 文件是否存在或无权访问 /// /// 相对路径或绝对路径 /// 如果是目录也返回false public static bool FileIsExist(string path) { return File.Exists(path); } /// /// 文件是否只读 /// /// /// public static bool FileIsReadOnly(string fullpath) { FileInfo file = new FileInfo(fullpath); if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { return true; } else { return false; } } /// /// 设置文件是否只读 /// /// /// true表示只读,反之 public static void SetFileReadonly(string fullpath, bool flag) { FileInfo file = new FileInfo(fullpath); if (flag) { // 添加只读属性 file.Attributes |= FileAttributes.ReadOnly; } else { // 移除只读属性 file.Attributes &= ~FileAttributes.ReadOnly; } } /// /// 取文件名 /// /// 文件路径 /// 是否移除后缀名 /// public static string GetFileName(string fullpath, bool removeExt) { FileInfo fi = new FileInfo(fullpath); string name = fi.Name; if (removeExt) { name = name.Remove(name.IndexOf('.')); } return name; } /// /// 取文件创建时间 /// /// /// public static DateTime GetFileCreateTime(string fullpath) { FileInfo fi = new FileInfo(fullpath); return fi.CreationTime; } /// /// 取文件最后存储时间 /// /// /// public static DateTime GetLastWriteTime(string fullpath) { FileInfo fi = new FileInfo(fullpath); return fi.LastWriteTime; } /// /// 创建一个零字节临时文件 /// /// public static string CreateTempZeroByteFile() { return Path.GetTempFileName(); } /// /// 创建一个随机文件名,不创建文件本身 /// /// public static string GetRandomFileName() { return Path.GetRandomFileName(); } /// /// 判断两个文件的哈希值是否一致 /// /// /// /// public static bool CompareFilesHash(string fileName1, string fileName2) { using (HashAlgorithm hashAlg = HashAlgorithm.Create()) { using (FileStream fs1 = new FileStream(fileName1, FileMode.Open), fs2 = new FileStream(fileName2, FileMode.Open)) { byte[] hashBytes1 = hashAlg.ComputeHash(fs1); byte[] hashBytes2 = hashAlg.ComputeHash(fs2); // 比较哈希码 return (BitConverter.ToString(hashBytes1) == BitConverter.ToString(hashBytes2)); } } } #endregion } }