using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Text; using System.Web; namespace Epost.Common { #region 文件 /// /// 文件 /// public static class FileHelper { /// /// 编码方式 /// private static readonly Encoding Encoding = Encoding.UTF8; #region 获取文件后缀名 /// /// 获取文件后缀名 /// /// /// /// public static string GetFileExt(string filePath, bool isPoint = false) { if (string.IsNullOrEmpty(filePath)) { return string.Empty; } if (filePath.LastIndexOf(".", StringComparison.Ordinal) > 0) { if (isPoint) { return filePath.Substring(filePath.LastIndexOf(".", StringComparison.Ordinal)); //文件扩展名,含“.” } else { return filePath.Substring(filePath.LastIndexOf(".", StringComparison.Ordinal) + 1); //文件扩展名,不含“.” } } return string.Empty; } #endregion #region 获取文件名 /// /// 获取文件名 /// /// /// /// public static string GetFileName(string filePath, bool isExt = true) { if (string.IsNullOrEmpty(filePath)) { return string.Empty; } var fileName = filePath.Substring(filePath.LastIndexOf(@"\", StringComparison.Ordinal) + 1); if (isExt) { return fileName; } fileName = fileName.Substring(0, fileName.LastIndexOf(".", StringComparison.Ordinal)); return fileName; } #endregion #region 获得当前绝对路径 /// /// 获得当前绝对路径 /// /// 指定的路径 /// 绝对路径 public static string GeAbsolutePath(string strPath) { if (strPath.ToLower().StartsWith("http://")) { return strPath; } if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(strPath); } else //非web程序引用 { strPath = strPath.Replace("/", "\\"); if (strPath.StartsWith("\\")) { strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\'); } return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } } #endregion #region 获得当前虚拟路径 /// /// 获得当前虚拟路径 /// /// 指定的路径 /// 绝对路径 public static string GeVirtualPath(string strPath) { if (strPath.ToLower().StartsWith("http://")) { return strPath; } if (HttpContext.Current != null) { return HttpContext.Current.Request.ApplicationPath + strPath; } else //非web程序引用 { strPath = strPath.Replace("/", "\\"); if (strPath.StartsWith("\\")) { strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\'); } return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } } #endregion #region 创建文件夹 /// /// 创建文件夹 /// /// 文件夹路径 /// 返回传来的路径 public static bool CreateFile(string path) { if (!Directory.Exists(path)) //检查文件是否存在 { Directory.CreateDirectory(path); //创建文件 } return Directory.Exists(path); } #endregion #region 判断文件是否存在 /// /// 判断文件是否存在 /// /// /// public static bool FileExists(string path) { return File.Exists(GeAbsolutePath(path)); } #endregion #region 复制文件夹 /// /// 复制文件夹 /// /// /// /// /// public static bool CopyDirectory(string sourceDir, string targetDir, List notInclude = null) { try { if (!Directory.Exists(sourceDir)) { return false; } if (!Directory.Exists(targetDir)) //检查文件是否存在 { Directory.CreateDirectory(targetDir); //创建文件 File.SetAttributes(targetDir, File.GetAttributes(sourceDir)); } if (targetDir[targetDir.Length - 1] != Path.DirectorySeparatorChar) targetDir = targetDir + Path.DirectorySeparatorChar; string[] files = Directory.GetFiles(sourceDir); foreach (string file in files) { if (notInclude != null && notInclude.Contains(file.ToLower())) { continue; } File.Copy(file, targetDir + Path.GetFileName(file), true); File.SetAttributes(targetDir + Path.GetFileName(file), FileAttributes.Normal); } string[] dirs = Directory.GetDirectories(sourceDir); foreach (string dir in dirs) { if (notInclude != null && notInclude.Contains(dir.ToLower())) { continue; } if (!CopyDirectory(dir, targetDir + Path.GetFileName(dir), notInclude)) { return false; } } return true; } catch (Exception ex) { // new LogHelper(typeof(FileHelper)).Error(ex); return false; } } #endregion #region 写入文件 /// /// 写入文件 /// /// 文件名 /// 文件内容 public static bool WriteFile(string filePath, string content) { try { var fs = new FileStream(filePath, FileMode.Create); Encoding encode = Encoding; //获得字节数组 byte[] data = encode.GetBytes(content); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); return true; } catch (Exception ex) { // new LogHelper(typeof(FileHelper)).Error(ex); return false; } } #endregion #region 读取文件 /// /// 读取文件 /// /// /// public static string ReadFile(string filePath) { return ReadFile(filePath, Encoding); } /// /// 读取文件 /// /// /// /// public static string ReadFile(string filePath, Encoding encoding) { try { using (var sr = new StreamReader(filePath, encoding)) { return sr.ReadToEnd(); } } catch (Exception ex) { // new LogHelper(typeof(FileHelper)).Error(ex); return string.Empty; } } #endregion } #endregion }