using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Web; namespace Pcb.Common { public static class PathUtils { public const char SeparatorChar = '\\'; private static string sRootPath = System.AppDomain.CurrentDomain.BaseDirectory; public const string uploadFileTypeCollectionForbidde = "cshtml,php,ade,adp,app,asa,asp,asmx,aspx,bas,bat,cdx,cer,cmd,com,cpl,crt,dll,exe,fxp,hlp,hta,htr,ins,isp,jse,jsp,lnk,mda,mdb,mde,mdt,mdw,mdz,msc,msi,msp,mst,ops,pcd,pif,prf,prg,reg,scf,scr,sct,shb,shs,url,vb,vbe,vbs,wsc,wsf,wsh"; public static string GetRootPath() { return sRootPath; } public static string GetUploadFileName(string filePath, bool isUploadChangeFileName = false) { string retval; if (isUploadChangeFileName) { DateTime now = DateTime.Now; string strDateTime = string.Format("{0}{1}{2}{3}{4}", now.Day, now.Hour, now.Minute, now.Second, now.Millisecond); retval = string.Format("{0}{1}", strDateTime, PathUtils.GetExtension(filePath)); } else { retval = Path.GetFileName(filePath); } return retval; } public static string Combine(params string[] paths) { string retval = string.Empty; if (paths != null && paths.Length > 0) { retval = (paths[0] != null) ? paths[0].Replace(PageUtils.SeparatorChar, PathUtils.SeparatorChar).TrimEnd(PathUtils.SeparatorChar) : string.Empty; for (int i = 1; i < paths.Length; i++) { string path = (paths[i] != null) ? paths[i].Replace(PageUtils.SeparatorChar, PathUtils.SeparatorChar).Trim(PathUtils.SeparatorChar) : string.Empty; retval = Path.Combine(retval, path); } } return retval; } public static string GetExtension(string path) { string retval = string.Empty; if (!string.IsNullOrEmpty(path)) { path = path.Trim('/', '\\').Trim(); try { retval = Path.GetExtension(path); } catch { } } return retval; } public static string RemoveExtension(string fileName) { string retval = string.Empty; if (!string.IsNullOrEmpty(fileName)) { int index = fileName.LastIndexOf('.'); if (index != -1) { retval = fileName.Substring(0, index); } else { retval = fileName; } } return retval; } public static bool IsFileExtenstionAllowed(string sAllowedExt, string sExt) { bool allow = false; if (sExt != null && sExt.StartsWith(".")) { sExt = sExt.Substring(1, sExt.Length - 1); } sAllowedExt = sAllowedExt.Replace("|", ","); string[] aExt = sAllowedExt.Split(','); for (int i = 0; i < aExt.Length; i++) { if (sExt.EqualsIgnoreCase(aExt[i])) { allow = true; break; } } return allow; } /// /// 是否危险文件 /// /// /// public static bool IsDangerousFile(this string sExt) { bool dangerous = false; if (sExt != null && sExt.StartsWith(".")) { sExt = sExt.Substring(1, sExt.Length - 1); } string[] aExt = uploadFileTypeCollectionForbidde.Replace("|", ",").Split(','); for (int i = 0; i < aExt.Length; i++) { if (sExt.EqualsIgnoreCase(aExt[i])) { dangerous = true; break; } } return dangerous; } /// /// 判断路径是否为绝对路径 /// /// /// public static bool IsAbsolutePath(string path) { if (!string.IsNullOrEmpty(path.Trim('/', '\\'))) { string regexPattern = @"^\s*([a-zA-Z]:\\|\\\\)([^\^\/:*?""<>|]+\\)*([^\^\/:*?""<>|]+)$"; Regex regex = new Regex(regexPattern); if (regex.IsMatch(path.Trim())) { return true; } } return false; } public static string GetSitePhysicalPath(string relativePath) { return HttpContext.Current.Server.MapPath("/" + relativePath); } } }