public static bool IsEmptyDirectory(string directoryPath) { bool result; try { string[] fileNames = DirFileHelper.GetFileNames(directoryPath); if (fileNames.Length > 0) { result = false; } else { string[] directoryNames = DirFileHelper.GetDirectories(directoryPath); if (directoryNames.Length > 0) { result = false; } else { result = true; } } } catch { result = true; } return(result); }
public static void DeleteDirectory(string directoryPath) { if (DirFileHelper.IsExistDirectory(directoryPath)) { Directory.Delete(directoryPath, true); } }
public static void CreateDirectory(string directoryPath) { if (!DirFileHelper.IsExistDirectory(directoryPath)) { Directory.CreateDirectory(directoryPath); } }
public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory) { Directory.CreateDirectory(varToDirectory); if (Directory.Exists(varFromDirectory)) { string[] directories = Directory.GetDirectories(varFromDirectory); if (directories.Length > 0) { string[] array = directories; for (int i = 0; i < array.Length; i++) { string d = array[i]; DirFileHelper.DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\"))); } } string[] files = Directory.GetFiles(varFromDirectory); if (files.Length > 0) { string[] array = files; for (int i = 0; i < array.Length; i++) { string s = array[i]; File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\\"))); } } } }
public static string[] GetFileNames(string directoryPath) { if (!DirFileHelper.IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } return(Directory.GetFiles(directoryPath)); }
public static void CreateFile(string dir, string pagestr) { dir = dir.Replace("/", "\\"); if (dir.IndexOf("\\") > -1) { DirFileHelper.CreateDir(dir.Substring(0, dir.LastIndexOf("\\"))); } StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir, false, Encoding.GetEncoding("GB2312")); sw.Write(pagestr); sw.Close(); }
public static void Move(string sourceFilePath, string descDirectoryPath) { string sourceFileName = DirFileHelper.GetFileName(sourceFilePath); if (DirFileHelper.IsExistDirectory(descDirectoryPath)) { if (DirFileHelper.IsExistFile(descDirectoryPath + "\\" + sourceFileName)) { DirFileHelper.DeleteFile(descDirectoryPath + "\\" + sourceFileName); } File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName); } }
public static void ClearDirectory(string directoryPath) { if (DirFileHelper.IsExistDirectory(directoryPath)) { string[] fileNames = DirFileHelper.GetFileNames(directoryPath); for (int i = 0; i < fileNames.Length; i++) { DirFileHelper.DeleteFile(fileNames[i]); } string[] directoryNames = DirFileHelper.GetDirectories(directoryPath); for (int i = 0; i < directoryNames.Length; i++) { DirFileHelper.DeleteDirectory(directoryNames[i]); } } }
public static void CreateFile(string filePath) { try { if (!DirFileHelper.IsExistFile(filePath)) { FileInfo file = new FileInfo(filePath); FileStream fs = file.Create(); fs.Close(); } } catch (Exception ex) { throw ex; } }
public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild) { bool result; try { string[] fileNames = DirFileHelper.GetFileNames(directoryPath, searchPattern, true); if (fileNames.Length == 0) { result = false; } else { result = true; } } catch (Exception ex) { throw new Exception(ex.Message); } return(result); }
public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild) { if (!DirFileHelper.IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } string[] files; try { if (isSearchChild) { files = Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { files = Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } return(files); }
public static void ClearFile(string filePath) { File.Delete(filePath); DirFileHelper.CreateFile(filePath); }