/// <summary> /// Deletes the specified directory and, if indicated, /// any subdirectories and files in the directory. /// </summary> /// <param name="path">The path.</param> /// <param name="user">The user.</param> /// <param name="recursive">if set to <c>true</c> [recursive].</param> /// <param name="debug">if set to <c>true</c> [debug].</param> /// <returns></returns> public static FileOperationResult DeleteDirectory(string path, DomainUser user, bool recursive = false, bool debug = false) { FileOperationResult result; // ReSharper disable once UnusedVariable using ( var impersonation = new Impersonation(user.Domain, user.Name, user.Pwd, ImpersonationLevel.Delegation)) { if (Directory.Exists(path)) { try { Directory.Delete(path, recursive); result = new FileOperationResult(path, FileOperation.Delete, true); } catch (Exception ex) { Log.Error(ex); result = new FileOperationResult(path, FileOperation.Delete, ex); } } else { result = new FileOperationResult(path, FileOperation.Delete, false, "Directory not Found"); } } return result; }
/// <summary> /// Determines whether the specified file exists. /// </summary> /// <returns> /// true if the caller has the required permissions and FullName contains the name of an existing file; /// otherwise, false. /// This method also returns false if FullName is null, an invalid path, /// or a zero-length string. /// If the caller does not have sufficient permissions to read the specified file, /// no exception is thrown and the method returns false regardless of the existence of FullName. /// </returns> public static bool Exists(this FileInfo fileInfo, DomainUser user) { // ReSharper disable once UnusedVariable using ( var impersonation = new Impersonation(user.Domain, user.Name, user.Pwd, ImpersonationLevel.Delegation)) { return File.Exists(fileInfo.FullName); } }
/// <summary> /// Copies the files. /// </summary> /// <param name="sourceFiles">The files.</param> /// <param name="destination">the copy target path</param> /// <param name="overwrite">determines if the files at target should be overwriten.</param> /// <param name="user">The user.</param> /// <param name="debug">if set to <c>true</c> [debug].</param> /// <returns></returns> public static List<FileOperationResult> CopyFiles( IEnumerable<FileInfo> sourceFiles, string destination, bool overwrite, DomainUser user, bool debug = false) { var results = new List<FileOperationResult>(); // ReSharper disable once UnusedVariable using ( var impersonation = new Impersonation(user.Domain, user.Name, user.Pwd, ImpersonationLevel.Delegation)) { results.AddRange(sourceFiles.Select(file => CopyFile(file, destination, overwrite, debug))); } return results; }
/// <summary> /// Deletes the file. /// </summary> /// <param name="file">The file.</param> /// <param name="user">The user.</param> /// <param name="debug">if set to <c>true</c> [debug].</param> /// <returns></returns> public static FileOperationResult DeleteFile(FileInfo file, DomainUser user, bool debug = false) { FileOperationResult result; // ReSharper disable once UnusedVariable using ( var impersonation = new Impersonation(user.Domain, user.Name, user.Pwd, ImpersonationLevel.Delegation)) { try { File.Delete(file.FullName); result = new FileOperationResult(file.FullName, FileOperation.Delete, true); } catch (Exception ex) { Log.Error(ex); result = new FileOperationResult(file.FullName, FileOperation.Delete, ex); } } return result; }
/// <summary> /// Gets the files at target. /// </summary> /// <param name="path">The path.</param> /// <param name="user">the Domain User</param> /// <param name="recursive"></param> /// <param name="debug">if set to <c>true</c> [debug].</param> /// <returns></returns> public static List<FileInfo> GetFiles(string path, DomainUser user, bool recursive, bool debug = false) { if (string.IsNullOrEmpty(user.Domain) || string.IsNullOrEmpty(user.Name) || string.IsNullOrEmpty(user.Pwd)) { return GetFiles(path, recursive, debug); } // ReSharper disable once UnusedVariable using ( var impersonation = new Impersonation(user.Domain, user.Name, user.Pwd, ImpersonationLevel.Delegation)) { return GetFiles(path, recursive, debug); } }