/// <summary> /// Delete a file. If the deleteEmptyDirectoresRecursively is true all empty directories /// from buttom up are also deleted. /// </summary> /// <param name="path"></param> /// <param name="deleteEmptyDirectoresRecursively"></param> public static void DeleteFile(string path, bool deleteEmptyDirectoresRecursively) { LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting file '{0}'", path)); C1File.Delete(path); if (deleteEmptyDirectoresRecursively) { string directory = Path.GetDirectoryName(path); string[] directories = directory.Split(Path.DirectorySeparatorChar); for (int i = directories.Length; i > 1; --i) { StringBuilder stringBuilder = new StringBuilder(); for (int j = 0; j < i; ++j) { stringBuilder.Append(directories[j]); stringBuilder.Append(Path.DirectorySeparatorChar); } string currentPath = stringBuilder.ToString(); if (C1Directory.GetFiles(currentPath).Length == 0) { LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting directory '{0}'", currentPath)); C1Directory.Delete(currentPath); } else { break; } } } }
/// <summary> /// Deletes all files recursively leaving all sub directories. /// </summary> /// <param name="directoryPath"></param> public static void DeleteFilesRecursively(string directoryPath) { foreach (string file in C1Directory.GetFiles(directoryPath)) { C1File.Delete(file); } foreach (string directory in C1Directory.GetDirectories(directoryPath)) { DeleteFilesRecursively(directory); } }
/// <exclude /> public static void RemoveReadOnlyRecursively(string directoryPath) { foreach (string file in C1Directory.GetFiles(directoryPath)) { FileUtils.RemoveReadOnly(file); } foreach (string directory in C1Directory.GetDirectories(directoryPath)) { RemoveReadOnlyRecursively(directory); } }
/// <summary> /// Ensures that the directory exists /// </summary> public static void EnsureDirectoryExists(string directory) { string[] directories = directory.Split(Path.DirectorySeparatorChar); if (directories.Length == 2) { return; } string currentPath = string.Format("{0}{1}", directories[0], Path.DirectorySeparatorChar); for (int i = 1; i < directories.Length; ++i) { currentPath = string.Format("{0}{1}{2}", currentPath, directories[i], Path.DirectorySeparatorChar); if (currentPath.StartsWith(PathUtil.BaseDirectory, StringComparison.InvariantCultureIgnoreCase)) // don't touch dirs outside our own folder! { if (C1Directory.Exists(currentPath) == false) { C1Directory.CreateDirectory(currentPath); } } } }
public static bool WritePermissionGranted(string fileOrDirectoryPath) { try { AuthorizationRuleCollection rules; if (C1File.Exists(fileOrDirectoryPath)) { FileSystemSecurity security = File.GetAccessControl(fileOrDirectoryPath); rules = security.GetAccessRules(true, true, typeof(NTAccount)); } else if (C1Directory.Exists(fileOrDirectoryPath)) { DirectorySecurity security = Directory.GetAccessControl(fileOrDirectoryPath); rules = security.GetAccessRules(true, true, typeof(NTAccount)); } else { throw new FileNotFoundException("File or directory '{0}' does not exist".FormatWith(fileOrDirectoryPath)); } var currentuser = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool result = false; foreach (FileSystemAccessRule rule in rules) { if ((rule.FileSystemRights & (FileSystemRights.WriteData | FileSystemRights.Write)) == 0) { continue; } if (rule.IdentityReference.Value.StartsWith("S-1-")) { var sid = new SecurityIdentifier(rule.IdentityReference.Value); if (!currentuser.IsInRole(sid)) { continue; } } else { if (!currentuser.IsInRole(rule.IdentityReference.Value)) { continue; } } if (rule.AccessControlType == AccessControlType.Deny) { return(false); } if (rule.AccessControlType == AccessControlType.Allow) { result = true; } } return(result); } catch { return(false); } }