public static string ReadFileStr(string vpath, bool lines = false) { FileEncodeHelper fileEncodeHelper = new FileEncodeHelper(); vpath = vpath.ToLower(); if (Enumerable.Count <string>((IEnumerable <string>)BanRead, (Func <string, bool>)(p => vpath.IndexOf(p) > -1)) > 0) { throw new Exception("该文件不允许读取!"); } string str1 = IOPath.VToP(vpath); if (!File.Exists(str1)) { throw new Exception(vpath + "文件不存在!"); } if (!lines) { return(File.ReadAllText(str1, fileEncodeHelper.GetType(str1))); } string str2 = ""; foreach (string str3 in File.ReadLines(str1, fileEncodeHelper.GetType(str1))) { str2 = str2 + str3 + "<br/>"; } return(str2); }
/// <summary> /// 移除文件或目录 /// </summary> /// <param name="vpath">文件或目录虚拟路径</param> /// <returns>true:删除成功</returns> public static bool DelFile(string vpath) { if (string.IsNullOrEmpty(vpath)) { return(false); } vpath = PathDeal(vpath).ToLower(); string path = IOPath.VToP(vpath); //是否禁止删除 foreach (string ban in BanDelDir) { if (vpath.StartsWith(ban)) { return(false); } } if (File.Exists(path)) { File.Delete(path); } else if (Directory.Exists(path)) { DirectoryInfo dir = new DirectoryInfo(path); DirectoryInfo[] childs = dir.GetDirectories(); foreach (DirectoryInfo child in childs) { child.Delete(true); } dir.Delete(true); } return(true); }
public static string WriteFile(string vpath, string content) { string name = ""; string path = ""; IOPath.GetNameAndPath(vpath, out name, out path); return(WriteFile(path, name, content)); }
/// <summary> /// 如不存在,则创建目录 /// </summary> /// <param name="vpath">虚拟目录路径,不包含文件名</param> public static void CreateDir(string vpath) { string ppath = IOPath.VToP(vpath); while (ppath.EndsWith("\\")) { ppath = ppath.TrimEnd('\\'); } int num = ppath.LastIndexOf("\\") + 1; CreateDir(ppath.Substring(0, num), ppath.Substring(num, ppath.Length - num)); }
//----------------下载文件|字符串 public static void DownFile(string vpath, string fname = "") { vpath = PathDeal(vpath); string str = IOPath.VToP(vpath); string exName = Path.GetExtension(vpath).ToLower(); if (Enumerable.Count <string>((IEnumerable <string>)BanDownName, (Func <string, bool>)(p => p.Equals(exName))) != 0 || !File.Exists(str)) { return; } DownLoadFile(str, fname, Encoding.UTF8); }
/// <summary> /// 保存文件(限定保存目录) /// </summary> public static string SaveFile(string vpath, string fileName, byte[] file) { DirPathDel(ref vpath); if (FileNameCheck(fileName) || !VPathCheck(vpath)) { throw new Exception(vpath + fileName + "保存失败,不符合命名规范!"); } string dirPath = IOPath.VToP(vpath.TrimEnd('/') + "/"); CreateDir(dirPath, ""); File.WriteAllBytes(dirPath + fileName, file); return(IOPath.PToV(dirPath + fileName)); }
/// <summary> /// 该路径的根目录是否允许写入 /// </summary> /// <param name="vpath">保存的虚拟路径</param> /// <returns>true:检测通过</returns> public static bool VPathCheck(string vpath) { string root = IOPath.GetPathRoot(vpath); for (int index = 0; index < BanPathName.Length; ++index) { if (BanPathName[index].Equals(root)) { return(false); } } return(true); }
/// <summary> /// 允许写入JS与html,config等文件 /// </summary> /// <param name="vpath"></param> /// <param name="content"></param> /// <returns></returns> public static string WriteUnSafeFile(string vpath, string content) { string name = ""; string path = ""; IOPath.GetNameAndPath(vpath, out name, out path); DirPathDel(ref vpath); string dirPath = IOPath.VToP(vpath); //if (SafeC.FileNameCheck(new string[1] { fileName }) || !SafeC.VPathCheck(vpath)) { throw new Exception(vpath + fileName + "保存失败,不符合命名规范!"); } CreateDir(dirPath, ""); string ppath = dirPath + name; byte[] bytes = Encoding.UTF8.GetBytes(content); File.WriteAllBytes(ppath, bytes); return(IOPath.PToV(ppath)); }
/// <summary> /// 更新Cert,如果不存在则新建 /// </summary> public static void Cert_Update() { if (!File.Exists(IOPath.VToP(certPath))) { certKey = Cert_NoExistThenNew(); } try { string key = ReadFileStr(certPath); if (!string.IsNullOrEmpty(key)) { key = EncryHelper.DesDecrypt(key); } else { key = ""; } certKey = key; } catch { certKey = ""; } }
public static byte[] ReadFileByte(string vpath) { vpath = vpath.ToLower(); if (Enumerable.Count <string>((IEnumerable <string>)BanRead, (Func <string, bool>)(p => vpath.IndexOf(p) > -1)) > 0) { throw new Exception("该文件不允许读取!"); } string path = IOPath.VToP(vpath); if (!File.Exists(path)) { throw new Exception(vpath + "文件不存在!"); } using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); fileStream.Close(); return(buffer); } }
/// <summary> /// 创建Cert并更新Key /// </summary> public static string Cert_NoExistThenNew() { string ppath = IOPath.VToP(certPath); string key = ""; if (!File.Exists(ppath)) { key = "ae" + IOPath.GetRandomString(14, 3).ToLower(); byte[] text = Encoding.UTF8.GetBytes(key); File.WriteAllBytes(ppath, text); } else { key = ReadFileStr(certPath); if (string.IsNullOrEmpty(key)) { key = "ae" + IOPath.GetRandomString(14, 3).ToLower(); byte[] text = Encoding.UTF8.GetBytes(key); File.WriteAllBytes(ppath, text); } } certKey = key; return(key); }
/// <summary> /// 是否为图片文件 /// </summary> public static bool IsImageFile(string fname) { return(IOPath.IsImageFile(fname)); }