/// <summary> /// 逐层创建文件夹 /// </summary> ///<param name="folderPath">文件夹相对路径</param> public static void CreateFolder(string folderPath) { try { folderPath = folderPath.Replace("\\", "/"); folderPath = folderPath.Replace("//", "/"); if (Utils2.RightStr(folderPath, 1) != "/") { folderPath += "/"; } if (!Directory.Exists(GetMapPath(folderPath))) { string[] strFolderPath = folderPath.Split('/'); string aimFolder = strFolderPath[0]; string CreatePath; for (int i = 1; i < strFolderPath.Length - 1; i++) { aimFolder = aimFolder + "/" + strFolderPath[i]; CreatePath = GetMapPath(aimFolder); if (!Directory.Exists(CreatePath)) { Directory.CreateDirectory(CreatePath); } } } } catch { throw; } }
/// <summary> /// 获得当前绝对路径 /// </summary> /// <param name="strPath">指定的路径</param> /// <returns>绝对路径</returns> public static string GetMapPath(string strPath) { if (strPath.IndexOf(":") == -1) { if (HttpContext.Current != null) { return(HttpContext.Current.Server.MapPath(strPath)); } else //非web程序引用 { strPath = strPath.Replace("\\", "/"); if (Utils2.LeftStr(strPath, 1) == "/") { strPath = strPath.Substring(1, strPath.Length - 1); } strPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); return(strPath.Replace("\\", "/")); } } else { return(strPath); } }
public static bool CheckNumberArray(string[] strNumber, int defaultValue) { if (strNumber == null) { return(false); } if (strNumber.Length < 1) { return(false); } foreach (string id in strNumber) { if (!IsNumber(id)) { return(false); } else { if (Utils2.StrToInt(id) <= defaultValue) { return(false); } } } return(true); }
/// <summary> /// 复制文件夹中的所有文件到指定文件夹 /// </summary> /// <param name="DirectoryPath">源文件夹路径</param> /// <param name="DirAddress">保存路径</param> /// <param name="DirFirst">true保留第一个文件夹目录,false不保留第一个文件夹目录</param> public static void CopyDirectory(string DirectoryPath, string DirAddress, bool DirFirst) //复制文件夹, { string s = DirectoryPath.Substring(DirectoryName(DirectoryPath)); //获取文件夹名 DirectoryInfo DirectoryArray = new DirectoryInfo(DirectoryPath); FileInfo[] Files = DirectoryArray.GetFiles(); //获取该文件夹下的文件列表 DirectoryInfo[] Directorys = DirectoryArray.GetDirectories(); //获取该文件夹下的文件夹列表 if (!DirFirst) { if (Directory.Exists(DirAddress)) { //Directory.Delete(DirAddress, true);//若文件夹存在,不管目录是否为空,删除 //Directory.CreateDirectory(DirAddress);//删除后,重新创建文件夹 } else { Directory.CreateDirectory(DirAddress); //文件夹不存在,创建 } foreach (FileInfo inf in Files) //逐个复制文件 { if (!File.Exists(DirAddress + "\\" + inf.Name)) { File.Copy(DirectoryPath + "\\" + inf.Name, DirAddress + "\\" + inf.Name); } } foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称,并递归调用方法本身 { CopyDirectory(DirectoryPath + "\\" + Dir.Name, DirAddress, true); } } else { if (Directory.Exists(DirAddress + "\\" + s)) { //Directory.Delete(DirAddress + "\\" + s, true);//若文件夹存在,不管目录是否为空,删除 //Directory.CreateDirectory(DirAddress + "\\" + s);//删除后,重新创建文件夹 } else { Utils2.CreateFolder(DirAddress); //Directory.CreateDirectory(DirAddress + "\\" + s);//文件夹不存在,创建 } foreach (FileInfo inf in Files)//逐个复制文件 { if (!File.Exists(DirAddress + "\\" + inf.Name)) { File.Copy(DirectoryPath + "\\" + inf.Name, DirAddress + "\\" + inf.Name); } } foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称,并递归调用方法本身 { CopyDirectory(DirectoryPath + "\\" + Dir.Name, DirAddress + "\\" + Dir.Name, false); } } }
/// <summary> /// 隐藏用户名,格式:a**b /// </summary> /// <param name="username"></param> /// <returns></returns> public static string HideUserName(string username) { if (!string.IsNullOrEmpty(username)) { return(Utils2.LeftStr(username, 1) + "**" + Utils2.RightStr(username, 1)); } else { return(""); } }
/// <summary> /// 从XML中读取数据 /// </summary> public static DataSet GetDataFromXml(string filePath) { filePath = Utils2.GetMapPath(filePath); if (!Utils2.FileExists(filePath)) { return(new DataSet()); } else { filePath = filePath.Replace("\\", "/"); if (filePath.IndexOf(":/") < 0) { filePath = Utils2.GetMapPath(filePath); } DataSet ds = new DataSet(); ds.ReadXml(filePath); return(ds); } }