文件操作类
/// <summary> /// 把一个文件夹下所有文件复制到另一个文件夹下 /// </summary> /// <param name="srcPath">当前文件夹</param> /// <param name="destPath">copy到新文件</param> public static void CopyDirectory(string srcPath, string destPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { if (!Directory.Exists(destPath + "\\" + i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹 } CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹 } else { File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件 } } } catch (Exception e) { throw; } } /// <summary> /// copy文件夹到另外一个文件夹下面 /// </summary> /// <param name="srcPath">需要copy的文件夹</param> /// <param name="destPath">copy的目标文件夹</param> /// <param name="notCopyFolder">不需要copy的文件夹</param> /// <param name="notCoyeFile">不需要copy的文件例如 web.config</param> public static void CopyDirectory(string srcPath, string destPath, List<string> notCopyFolder, List<string> notCopyFile) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { var cf = notCopyFolder.Where(x => x.Contains(i.Name.ToUpper())).ToList(); if (cf.Count > 0) { continue; } if (!Directory.Exists(destPath + "\\" + i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹 } CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹 } else { var tf = notCopyFile.Where(x => x.Contains(i.Name)).ToList(); if (tf.Count > 0) { continue; } File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件 } } } catch (Exception e) { throw; } } /// <summary> ///删除目录下的所有文件及文件夹 /// </summary> /// <param name="strPath"></param> public static void DeleteDirFiles(string strPath) { FileAttributes attr = File.GetAttributes(strPath); if (attr == FileAttributes.Directory) { Directory.Delete(strPath, true); } else { File.Delete(strPath); } } /// <summary> /// 根据路径删除文件 /// </summary> /// <param name="path"></param> public static void DeleteFile(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } } /// <summary> ///路径转换为相对路径 /// </summary> /// <param name="imageurl1"></param> /// <returns></returns> public static string UrlConvertToR(string url) { string tmpRootDir = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()); //获取程序根目录 string imageurl2 = url.Replace(tmpRootDir, ""); //转换成相对路径 imageurl2 = imageurl2.Replace(@"\", @"/"); return imageurl2; } /// <summary> /// 删除目录下的所有文件及文件夹 /// </summary> /// <param name="strPath"></param> public static void DeleteFilePath(string strPath) { //删除这个目录下的所有子目录 if (Directory.GetDirectories(strPath).Length > 0) { foreach (string var in Directory.GetDirectories(strPath)) { Directory.Delete(var, true); } } //删除这个目录下的所有文件 if (Directory.GetFiles(strPath).Length > 0) { foreach (string var in Directory.GetFiles(strPath)) { File.Delete(var); } } } /// <summary> /// 得到文件夹 /// </summary> /// <param name="Path">The path.</param> /// <returns></returns> public static FileInfo[] GetFileInfos(string path) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] inf = dir.GetFiles(); return inf; } /// <summary> /// 文件重新命名 /// </summary> /// <param name="deFaultFileName"></param> /// <param name="newFileName"></param> public static void FileRename(string deFaultFileName, string newFileName) { FileInfo fi = new FileInfo(deFaultFileName); fi.MoveTo(Path.Combine(newFileName)); }