public static bool TryDelete(FileSystemInfo info) { if (!info.Exists) { return false; } var success = true; var dir = info as DirectoryInfo; if (dir != null) { var children = dir .GetFiles() .OfType<FileSystemInfo>() .Concat(dir.GetDirectories()); foreach (var f in children) { if (!TryDelete(f)) { success = false; } } } info.Delete(); return success; }
private static void RecreateTestDb(string testDbName, SqlConnection conn, FileSystemInfo mdfFile, FileSystemInfo ldfFile, string path) { new SqlCommand(string.Format( "IF EXISTS(SELECT name FROM sys.databases WHERE name = '{0}') DROP DATABASE {0}", testDbName), conn) .ExecuteNonQuery(); if (mdfFile.Exists) mdfFile.Delete(); if (ldfFile.Exists) ldfFile.Delete(); new SqlCommand(string.Format("CREATE DATABASE {1} ON PRIMARY (Name={1}, filename = '{0}{1}.mdf')", path, testDbName), conn).ExecuteNonQuery(); }
protected static void DeleteFileSystemInfo(FileSystemInfo fsi) { fsi.Attributes = FileAttributes.Normal; var di = fsi as DirectoryInfo; if (di != null) foreach (var dirInfo in di.GetFileSystemInfos()) DeleteFileSystemInfo(dirInfo); fsi.Delete(); }
protected static void DeletePlatformItem(FileSystemInfo platformItem) { // If platformItem is a directory, then force delete its subdirectories var directory = platformItem as DirectoryInfo; if (directory != null) { directory.Delete(true); return; } platformItem.Delete(); }
private void Delete(FileSystemInfo file) { try { file.Delete(); } catch (Exception) { logger.Error("Could not delete " + file.Name); throw; } }
private static void Delete(FileSystemInfo file) { // Setup initial conditions. if (!file.Exists) return; var path = file.FullName; // Remove the read-only attribute. if (File.GetAttributes(path) == FileAttributes.ReadOnly) File.SetAttributes(path, FileAttributes.Normal); // Finish up. file.Delete(); }
public static void Delete(FileSystemInfo fileSystemInfo) { DirectoryInfo Dir = fileSystemInfo as DirectoryInfo; if (Dir != null) { foreach (FileSystemInfo Child in Dir.GetFileSystemInfos()) { Delete(Child); } } // Delete the root info fileSystemInfo.Delete(); }
private static void DeleteFileSystemInfo(FileSystemInfo fileSystemInfo) { var directoryInfo = fileSystemInfo as DirectoryInfo; if (directoryInfo != null) { foreach (var childInfo in directoryInfo.GetFileSystemInfos()) { DeleteFileSystemInfo(childInfo); } } fileSystemInfo.Attributes = FileAttributes.Normal; fileSystemInfo.Delete(); }
private static void DeleteFileSystemInfo(FileSystemInfo fsi) { CheckIfDeleteIsValid(fsi); fsi.Attributes = FileAttributes.Normal; var di = fsi as DirectoryInfo; if (di != null) { foreach (FileSystemInfo dirInfo in di.GetFileSystemInfos()) { DeleteFileSystemInfo(dirInfo); } } fsi.Delete(); }
public override void Apply(FileSystemInfo fsi, Environment environment) { if (!environment.IsSimulating) { try { fsi.Delete(); Log.Info("Delete {0}... OK", fsi.FullName); } catch (Exception e) { Log.Warning("Delete {0}... {1}", fsi.FullName, e.Message); } } else { Log.Info("Delete {0}", fsi.FullName); } }
public static void Delete(FileSystemInfo fileSystemInfo) { DirectoryInfo Dir = fileSystemInfo as DirectoryInfo; if (Dir != null && Dir.Exists) { foreach (FileSystemInfo Child in Dir.GetFileSystemInfos()) { Delete(Child); System.Threading.Thread.Sleep(2); } } // Delete the root info if (fileSystemInfo.Exists) { fileSystemInfo.Attributes = FileAttributes.Normal; fileSystemInfo.Delete(); } }
/// <summary> /// Wipe the log /// </summary> /// <param name="logFile"></param> private static void CleanLog(FileSystemInfo logFile) { try { logFile.Delete(); } catch (Exception ex) { Error(LogName, "Failed to delete log file"); Error(LogName, ex.Message); } }
private static void DeleteLocalItemRecursive(FileSystemInfo source) { if (source is DirectoryInfo) { foreach (var child in (source as DirectoryInfo).EnumerateFileSystemInfos()) { DeleteLocalItemRecursive(child); } } source.Delete(); }
/// <summary> /// Remove local item /// </summary> private bool RemoveLocalItem(FileSystemInfo item) { item.Delete(); return true; }
private static void Delete(FileSystemInfo f) { f.Delete(); }
void DeleteRecursive(FileSystemInfo fsi) { fsi.Attributes = FileAttributes.Normal; var di = fsi as DirectoryInfo; if (di != null) { foreach (var dirInfo in di.GetFileSystemInfos()) { DeleteRecursive(dirInfo); } } fsi.Delete(); }
protected void DeleteFileIfExists(FileSystemInfo file) { if (file.Exists){ Report("Deleting " + file.FullName); if (file is DirectoryInfo) ((DirectoryInfo)file).Delete(true); else file.Delete(); } }
} // RemoveFileInfoItem /// <summary> /// Removes the file system object from the file system. /// </summary> /// /// <param name="fileSystemInfo"> /// The FileSystemInfo object representing the file or directory to be removed. /// </param> /// /// <param name="force"> /// If true, the readonly and hidden attributes will be masked off in the case of /// an error, and the removal will be attempted again. If false, exceptions are /// written to the error pipeline. /// </param> /// private void RemoveFileSystemItem(FileSystemInfo fileSystemInfo, bool force) { Dbg.Diagnostics.Assert( fileSystemInfo != null, "Caller should always check fileSystemInfo"); //First check if we can delete this file when force is not specified. if (!Force && (fileSystemInfo.Attributes & (FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly)) != 0) { String error = StringUtil.Format(FileSystemProviderStrings.PermissionError); Exception e = new IOException(error); ErrorDetails errorDetails = new ErrorDetails(this, "FileSystemProviderStrings", "CannotRemoveItem", fileSystemInfo.FullName, e.Message); ErrorRecord errorRecord = new ErrorRecord(e, "RemoveFileSystemItemUnAuthorizedAccess", ErrorCategory.PermissionDenied, fileSystemInfo); errorRecord.ErrorDetails = errorDetails; WriteError(errorRecord); return; } // Store the old attributes in case we fail to delete FileAttributes oldAttributes = fileSystemInfo.Attributes; bool attributeRecoveryRequired = false; try { // Try to delete the item. Strip any problematic attributes // if they've specified force. if (force) { fileSystemInfo.Attributes = fileSystemInfo.Attributes & ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); attributeRecoveryRequired = true; } fileSystemInfo.Delete(); if (force) { attributeRecoveryRequired = false; } } catch (Exception fsException) { CommandProcessorBase.CheckForSevereException(fsException); ErrorDetails errorDetails = new ErrorDetails(this, "FileSystemProviderStrings", "CannotRemoveItem", fileSystemInfo.FullName, fsException.Message); if ((fsException is System.Security.SecurityException) || (fsException is UnauthorizedAccessException)) { ErrorRecord errorRecord = new ErrorRecord(fsException, "RemoveFileSystemItemUnAuthorizedAccess", ErrorCategory.PermissionDenied, fileSystemInfo); errorRecord.ErrorDetails = errorDetails; WriteError(errorRecord); } else if (fsException is ArgumentException) { ErrorRecord errorRecord = new ErrorRecord(fsException, "RemoveFileSystemItemArgumentError", ErrorCategory.InvalidArgument, fileSystemInfo); errorRecord.ErrorDetails = errorDetails; WriteError(errorRecord); } else if ((fsException is IOException) || (fsException is FileNotFoundException) || (fsException is DirectoryNotFoundException)) { ErrorRecord errorRecord = new ErrorRecord(fsException, "RemoveFileSystemItemIOError", ErrorCategory.WriteError, fileSystemInfo); errorRecord.ErrorDetails = errorDetails; WriteError(errorRecord); } else { throw; } } finally { if (attributeRecoveryRequired) { try { if (fileSystemInfo.Exists) { fileSystemInfo.Attributes = oldAttributes; } } catch (Exception attributeException) { CommandProcessorBase.CheckForSevereException(attributeException); if ((attributeException is System.IO.DirectoryNotFoundException) || (attributeException is System.Security.SecurityException) || (attributeException is System.ArgumentException) || (attributeException is System.IO.FileNotFoundException) || (attributeException is System.IO.IOException)) { ErrorDetails attributeDetails = new ErrorDetails( this, "FileSystemProviderStrings", "CannotRestoreAttributes", fileSystemInfo.FullName, attributeException.Message); ErrorRecord errorRecord = new ErrorRecord(attributeException, "RemoveFileSystemItemCannotRestoreAttributes", ErrorCategory.PermissionDenied, fileSystemInfo); errorRecord.ErrorDetails = attributeDetails; WriteError(errorRecord); } else throw; } } } } // RemoveFileSystemItem
private void RemoveFileSystemItem(FileSystemInfo fileSystemInfo, bool force) { if ((base.Force == 0) && ((fileSystemInfo.Attributes & (System.IO.FileAttributes.System | System.IO.FileAttributes.Hidden | System.IO.FileAttributes.ReadOnly)) != 0)) { Exception exception = new IOException(StringUtil.Format(FileSystemProviderStrings.PermissionError, new object[0])); ErrorDetails details = new ErrorDetails(this, "FileSystemProviderStrings", "CannotRemoveItem", new object[] { fileSystemInfo.FullName, exception.Message }); ErrorRecord errorRecord = new ErrorRecord(exception, "RemoveFileSystemItemUnAuthorizedAccess", ErrorCategory.PermissionDenied, fileSystemInfo) { ErrorDetails = details }; base.WriteError(errorRecord); } else { System.IO.FileAttributes attributes = fileSystemInfo.Attributes; bool flag = false; try { if (force) { fileSystemInfo.Attributes &= ~(System.IO.FileAttributes.System | System.IO.FileAttributes.Hidden | System.IO.FileAttributes.ReadOnly); flag = true; } fileSystemInfo.Delete(); if (force) { flag = false; } } catch (Exception exception2) { CommandProcessorBase.CheckForSevereException(exception2); ErrorDetails details2 = new ErrorDetails(this, "FileSystemProviderStrings", "CannotRemoveItem", new object[] { fileSystemInfo.FullName, exception2.Message }); if ((exception2 is SecurityException) || (exception2 is UnauthorizedAccessException)) { ErrorRecord record2 = new ErrorRecord(exception2, "RemoveFileSystemItemUnAuthorizedAccess", ErrorCategory.PermissionDenied, fileSystemInfo) { ErrorDetails = details2 }; base.WriteError(record2); } else if (exception2 is ArgumentException) { ErrorRecord record3 = new ErrorRecord(exception2, "RemoveFileSystemItemArgumentError", ErrorCategory.InvalidArgument, fileSystemInfo) { ErrorDetails = details2 }; base.WriteError(record3); } else { if ((!(exception2 is IOException) && !(exception2 is FileNotFoundException)) && !(exception2 is DirectoryNotFoundException)) { throw; } ErrorRecord record4 = new ErrorRecord(exception2, "RemoveFileSystemItemIOError", ErrorCategory.WriteError, fileSystemInfo) { ErrorDetails = details2 }; base.WriteError(record4); } } finally { if (flag) { try { if (fileSystemInfo.Exists) { fileSystemInfo.Attributes = attributes; } } catch (Exception exception3) { CommandProcessorBase.CheckForSevereException(exception3); if ((!(exception3 is DirectoryNotFoundException) && !(exception3 is SecurityException)) && ((!(exception3 is ArgumentException) && !(exception3 is FileNotFoundException)) && !(exception3 is IOException))) { throw; } ErrorDetails details3 = new ErrorDetails(this, "FileSystemProviderStrings", "CannotRestoreAttributes", new object[] { fileSystemInfo.FullName, exception3.Message }); ErrorRecord record5 = new ErrorRecord(exception3, "RemoveFileSystemItemCannotRestoreAttributes", ErrorCategory.PermissionDenied, fileSystemInfo) { ErrorDetails = details3 }; base.WriteError(record5); } } } } }
static void Delete(FileSystemInfo fileSystemInfo) { if ((fileSystemInfo.Attributes & FileAttributes.ReparsePoint) == 0) { var di = fileSystemInfo as DirectoryInfo; if (di != null) foreach (var fsi in di.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly)) Delete(fsi); } fileSystemInfo.Attributes = FileAttributes.Normal; fileSystemInfo.Delete(); }
private void DeleteFile(FileSystemInfo file) { file.Attributes = FileAttributes.Normal; file.Delete(); }
private static void DeleteFileSystemInfo(FileSystemInfo fileSystemInfo) { var directoryInfo = fileSystemInfo as DirectoryInfo; if (directoryInfo != null) { foreach (var childInfo in directoryInfo.GetFileSystemInfos()) { DeleteFileSystemInfo(childInfo); } } fileSystemInfo.Attributes = FileAttributes.Normal; // thumbnails can be intentionally readonly (when they are created by hand) fileSystemInfo.Delete(); }
public void DeleteFileSystemObject(FileSystemInfo fsi) { try { if (fsi.Exists) { DirectoryInfo di = fsi as DirectoryInfo; if (di != null) { PathUtils.DeleteFolderTree(fsi.FullName); // File system changed => refresh will be required this.RequiresRefresh = true; return; } fsi.Attributes ^= fsi.Attributes; fsi.Delete(); // File system changed => refresh will be required this.RequiresRefresh = true; } } catch (Exception ex) { _task.AddToErrorMap(fsi.FullName, ex.Message); } }
private void DeleteFileSystemInfo(FileSystemInfo fsi) { if (this.dryRun) fsi.Attributes = FileAttributes.Normal; var di = fsi as DirectoryInfo; this.deletedFiles++; if (di != null) foreach (var dirInfo in di.GetFileSystemInfos()) DeleteFileSystemInfo(dirInfo); if (!this.dryRun) fsi.Delete(); }
/// <summary> /// Deletes the file or directory if it exists. /// </summary> /// <param name="info">FileInfo or DirectoryInfo instance</param> public void Delete(FileSystemInfo info) { if (info.Exists) info.Delete(); info.Refresh(); }
/// <summary> /// Delete file or directory /// </summary> /// <param name="fsi"></param> private static void DeleteFileSystemInfo(FileSystemInfo fileSystemInfo) { // ensure not read-only fileSystemInfo.Attributes = FileAttributes.Normal; // loop through children, if directory if (fileSystemInfo is DirectoryInfo) foreach (var childFileSystemInfo in ((DirectoryInfo)fileSystemInfo).GetFileSystemInfos()) DeleteFileSystemInfo(childFileSystemInfo); // delete fileSystemInfo.Delete(); }
private void DeleteFileSystemInfo(FileSystemInfo info) { info.CreationTime = info.LastWriteTime = info.LastAccessTime = MinTimestamp; info.Attributes = FileAttributes.Normal; info.Attributes = FileAttributes.NotContentIndexed; for (int i = 0, tries = 0; i < FileNameErasePasses; ++tries) { string newPath = GenerateRandomFileName(info.GetParent(), info.Name.Length); try { info.MoveTo(newPath); ++i; } catch (IOException e) { switch (System.Runtime.InteropServices.Marshal.GetLastWin32Error()) { case Win32ErrorCode.AccessDenied: throw new UnauthorizedAccessException(S._("The file {0} could not " + "be erased because the file's permissions prevent access to the file.", info.FullName)); case Win32ErrorCode.SharingViolation: if (tries > FileNameEraseTries) throw new IOException(S._("The file {0} is currently in use and " + "cannot be removed.", info.FullName), e); Thread.Sleep(100); break; default: throw; } } } for (int i = 0; i < FileNameEraseTries; ++i) try { info.Delete(); break; } catch (IOException e) { switch (System.Runtime.InteropServices.Marshal.GetLastWin32Error()) { case Win32ErrorCode.AccessDenied: throw new UnauthorizedAccessException(S._("The file {0} could not " + "be erased because the file's permissions prevent access to the file.", info.FullName), e); case Win32ErrorCode.SharingViolation: if (i > FileNameEraseTries) throw new IOException(S._("The file {0} is currently in use and " + "cannot be removed.", info.FullName), e); Thread.Sleep(100); break; default: throw; } } }
/// <summary> /// Wipe the log /// </summary> /// <param name="logFile"></param> private static void CleanLog(FileSystemInfo logFile) { if (logFile == null) throw new ArgumentNullException(nameof(logFile)); try { logFile.Delete(); } catch (Exception ex) { Error(LogName, "Failed to delete log file"); Error(LogName, ex.Message); } }