/// <summary> /// Delete All folder and files with in a folder /// </summary> /// <param name="destdir">folder path need empty</param> public static bool EmptyFolder(string destdir) { try { if (Directory.Exists(destdir) == true) { //Method to Bypass User Access Control (UAC) by assigning permissions. AFolderFilesPermission.AddFolderSecurity(destdir); foreach (string ff in Directory.GetFiles(destdir)) { AFolderFilesPermission.AddFileSecurity(ff); File.Delete(ff); } foreach (string d in Directory.GetDirectories(destdir)) { AFolderFilesPermission.AddFolderSecurity(d); Directory.Delete(d, true); } return(true); } else { return(false); } } #pragma warning disable CS0168 // The variable 'ex' is declared but never used catch (IOException ex) #pragma warning restore CS0168 // The variable 'ex' is declared but never used { return(false); } }
/// <summary> /// Overwrite file /// </summary> /// <param name="source"> source file</param> /// <param name="destination">destination file</param> /// <returns></returns> public static string ReplaceFile(string source, string destination) { string s = "Replace file successfully"; if (File.Exists(source) == true) { if (File.Exists(destination) == true) { AFolderFilesPermission.AddFileSecurity(destination); File.Delete(destination); } if (new FileInfo(destination).Directory.Exists == true) { AFolderFilesPermission.AddFileSecurity(source); File.Copy(source, destination, true); } else { s = "Missing Destination Directory"; } return(s); } else { return("Source File Missing"); } }
/// <summary> /// Copy FIle to another place /// </summary> /// <param name="source">Source file</param> /// <param name="destination">Destination file</param> /// <returns></returns> public static string CopyFile(string source, string destination) { if (File.Exists(source) == true) { if (new FileInfo(destination).Directory.Exists == true) { AFolderFilesPermission.AddFileSecurity(source); File.Copy(source, destination); return("Copy Successfully!"); } else { return("Destination Folder Not Found"); } } else { return("Source File Missing"); } }
/// <summary> /// Replace file and take old file backup also /// </summary> /// <param name="source">source file</param> /// <param name="destination">Destinaion file</param> /// <returns></returns> public static string ReplaceFileWithFileBackupAndMoveSourcefile(string source, string destination) { if (File.Exists(source) == true) { if (File.Exists(destination) == true) { AFolderFilesPermission.AddFileSecurity(source); AFolderFilesPermission.AddFileSecurity(destination); File.Replace(source, destination, destination + ".bac", true); return("Replace Successfully!"); } else { return("Destination File Not Found"); } } else { return("Source File Missing"); } }
/// <summary> /// Delete File /// </summary> /// <param name="file">target file</param> /// <returns></returns> public static bool DeleteFile(string file) { try { if (File.Exists(file) == true) { //Method to Bypass User Access Control (UAC) by assigning permissions. AFolderFilesPermission.AddFileSecurity(file); File.Delete(file); return(true); } else { return(false); } } #pragma warning disable CS0168 // The variable 'e' is declared but never used catch (Exception e) #pragma warning restore CS0168 // The variable 'e' is declared but never used { return(false); } }