using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Net.Template.Common { /// <summary> /// 文件操作类 /// </summary> public class FilesHelper { /// <summary> /// /// </summary> public FilesHelper() { } #region 拷贝目录 /// <summary> /// 拷贝目录 /// </summary> /// <param name="sourceDirName">原目录</param> /// <param name="destDirName">新目录</param> public static void CopyDirectory(string sourceDirName, string destDirName) { try { if (!Directory.Exists(destDirName))//新目录不存在,则创建新目录 { Directory.CreateDirectory(destDirName); } DirectoryInfo dir = new DirectoryInfo(sourceDirName); //获取所有文件夹和文件 FileSystemInfo[] sfiles = dir.GetFileSystemInfos(); if (sfiles != null && sfiles.Length > 0) { for (int i = 0; i < sfiles.Length; i++) { if (sfiles[i].Attributes == FileAttributes.Directory) { CopyDirectory(sfiles[i].FullName, destDirName + "\\" + sfiles[i].Name); } else { FileInfo file = (FileInfo)sfiles[i]; file.CopyTo(destDirName + "\\" + file.Name, true); } } } }catch(Exception ex) { throw ex; } } #endregion #region 拷贝文件 /// <summary> /// 拷贝文件 /// </summary> /// <param name="oldFile">原始文件</param> /// <param name="newFile">新文件路径</param> /// <param name="overwrite">是否重写</param> public static void CopyFile(string oldFile, string newFile,bool overwrite) { try { File.Copy(oldFile, newFile, overwrite); }catch(Exception ex) { throw ex; } } #endregion #region 追加文件内容 /// <summary> /// 追加文件内容 /// </summary> /// <param name="path">文件路径</param> /// <param name="str">内容</param> /// <param name="encode">页面编码</param> public static void AppendTextFile(string path, string str, string encode) { try { if (!File.Exists(path)) throw new Exception("指定的文件不存在"); StreamWriter sw = new StreamWriter(path, true, Encoding.GetEncoding(encode)); sw.Write(str); sw.Flush(); sw.Close(); }catch(Exception ex) { throw ex; } } #endregion #region 读文件 /// <summary> /// 读文件 /// </summary> /// <param name="path">文件路径</param> /// <param name="encode">页面编码</param> /// <returns></returns> public static string ReadFile(string path, string encode) { string str = string.Empty; if (!File.Exists(path)) throw new Exception("指定的文件不存在"); try { StreamReader sr = new StreamReader(path, Encoding.GetEncoding(encode)); str = sr.ReadToEnd(); sr.Close(); sr.Dispose(); }catch(Exception ex) { throw ex; } return str; } #endregion #region 写文件 /// <summary> /// 写文件 /// </summary> /// <param name="path">文件路径</param> /// <param name="str">文件内容</param> /// <param name="encode">页面编码</param> public static void WriteFile(string path, string str,string encode) { try { if (!File.Exists(path)) { FileStream f = File.Create(path); f.Close(); } StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding(encode)); sw.Write(str); sw.Close(); sw.Dispose(); }catch(Exception ex) { throw ex; } } #endregion #region 得到文件名的后缀名,有包括小数点,如.txt /// <summary> /// 得到文件名的后缀名,有包括小数点,如.txt /// </summary> /// <param name="fileName">文件名</param> /// <returns></returns> public static string GetPostfix(string fileName) { int start = fileName.LastIndexOf("."); int length = fileName.Length; string postfix = fileName.Substring(start, length - start); return postfix; } #endregion #region 创建文件夹 /// <summary> /// 创建文件夹 /// </summary> /// <param name="path">绝对路径</param> public static void CreateDirectory(string path) { if (path == null || path.Trim().Length.Equals("")) throw new Exception("请输入合法的创建文件夹路径"); try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch(Exception ex) { throw ex; } } #endregion #region 删除文件夹及文件夹下面所有文件 /// <summary> /// 删除文件夹及文件夹下面所有文件 /// </summary> /// <param name="path">绝对路径</param> public static void DeleteDirectory(string path) { try { DirectoryInfo directoryInfo = new DirectoryInfo(path); if (directoryInfo.Exists) { directoryInfo.Delete(true); } } catch(Exception ex) { throw ex; } } #endregion #region 删除文件 /// <summary> /// 删除文件 /// </summary> /// <param name="file">绝对路径</param> public static void DeleteFile(string file) { IList<string> list = new List<string>(); list.Add(file); DeleteFile(list); } #endregion #region 批量删除文件 /// <summary> /// 批量删除文件 /// </summary> /// <param name="files">绝对路径</param> public static void DeleteFile(IList<string> files) { try { foreach (string str in files) { System.IO.File.Delete(str); } } catch (Exception ex) { throw ex; } } #endregion #region 得到路径的容量(单位B),包括子目录 /// <summary> /// 得到路径的容量(单位B) /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static long GetFileSize(string filePath) { long temp = 0; //判断当前路径所指向的是否为文件 if (File.Exists(filePath) == false) { string[] str = Directory.GetFileSystemEntries(filePath); foreach (string s in str) { temp += GetFileSize(s); } } else { //定义一个FileInfo对象,使之与filePath所指向的文件向关联, //以获取其大小 FileInfo fileInfo = new FileInfo(filePath); return fileInfo.Length; } return temp; } #endregion #region 返回容量,如:32MB /// <summary> /// 返回容量,如:32MB /// </summary> /// <param name="length">总长度(单位B)</param> ///<param name="num">显示数值</param> ///<param name="str">显示的单位(B,KB,MB)</param> public static void GetUnit(long length, out double num, out string str) { if (length > (1024 * 1024 * 1024)) //由大向小来判断文件的大小 { //将双精度浮点数舍入到指定的小数(long类型与double类型运算,结果会是一个double类型) num = Math.Round((length + 0.00) / (1024 * 1024 * 1024), 2); str = " GB"; } else if (length > (1024 * 1024)) { num = Math.Round((length + 0.00) / (1024 * 1024), 2); str = " MB"; } else if (length > 1024) { num = Math.Round((length + 0.00) / 1024, 2); str = " KB"; } else { num = length; str = " K"; } } #endregion #region 返回所给文件路径的字节数组 /// <summary> /// 返回所给文件路径的字节数组 /// </summary> /// <param name="fileName">文件绝对路径</param> /// <returns></returns> public static byte[] GetBinaryFile(string fileName) { if (File.Exists(fileName)) { try { //打开现有文件以进行读取。 FileStream s = File.OpenRead(fileName); byte[] bt = ConvertStreamToByteBuffer(s); s.Close(); return bt; } catch { return new byte[0]; } } else { return new byte[0]; } } #endregion #region 把给定的文件流转换为二进制字节数组 /// <summary> /// 把给定的文件流转换为二进制字节数组 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static byte[] ConvertStreamToByteBuffer(Stream stream) { //求出总长度 long length = stream.Length; //定义字节数组长度 int arrayLen = 1000; //获取总长度可供字节长度读取整数倍 long pers = length / arrayLen; //获取剩余字节 long sy = length - arrayLen * pers; //定义字节数组 byte[] bt = new byte[arrayLen]; byte[] tempByte = new byte[length]; BinaryReader br = new BinaryReader(stream, Encoding.UTF8); //先读取整数倍 for (int i = 0; i < pers; i++) { br.Read(tempByte, i * arrayLen, arrayLen); } //读取剩余部分 if (sy > 0) { br.Read(tempByte, (int)pers * arrayLen, (int)sy); } br.Close(); return tempByte; } #endregion } }