Beispiel #1
0
        public static void SecureDeleteDirectory(String path, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate)
            {
                return;
            }

            if (deleteMode == DeleteModes.Direct)
            {
                Directory.Delete(path, recursive: false, ignoreReadOnly: true);   //throws IOException if not empty anymore

                return;
            }

            // Last security check before deletion
            if (Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0)
            {
                if (deleteMode == DeleteModes.RecycleBin)
                {
                    // Check CLR permissions -> could raise a exception
                    new FileIOPermission(FileIOPermissionAccess.Write, path + Path.DirectorySeparatorChar).Demand();

                    //if (!CheckWriteAccess(Directory.GetAccessControl(path)))
                    if (IsDirLocked(path))
                    {
                        throw new REDPermissionDeniedException("Could not delete directory \"" + path +
                                                               "\" because the access is protected by the (file) system (permission denied).");
                    }

                    FileSystem.DeleteDirectory(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                }
                else if (deleteMode == DeleteModes.RecycleBinShowErrors)
                {
                    FileSystem.DeleteDirectory(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                }
                else if (deleteMode == DeleteModes.RecycleBinWithQuestion)
                {
                    FileSystem.DeleteDirectory(path, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                }
                else
                {
                    throw new Exception("Internal error: Unknown delete mode: \"" + deleteMode + "\"");
                }
            }
            else
            {
                throw new Exception("Aborted deletion of the directory \"" + path +
                                    "\" because it is no longer empty. This can happen if RED previously failed to delete a empty (trash) file.");
            }
        }
Beispiel #2
0
        public static void ManuallyDeleteDirectory(string path, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate)
            {
                return;
            }

            if (path == "")
            {
                throw new Exception("Could not delete directory because the path was empty.");
            }

            //TODO: Add FileIOPermission code?

            FileSystem.DeleteDirectory(path, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
        }
Beispiel #3
0
        public static void SecureDeleteFile(FileInfo file, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate)
            {
                return;
            }

            if (deleteMode == DeleteModes.RecycleBin)
            {
                // Check CLR permissions -> could raise a exception
                new FileIOPermission(FileIOPermissionAccess.Write, file.FullName).Demand();

                if (IsFileLocked(file))
                {
                    throw new REDPermissionDeniedException("Could not delete file \"" + file.FullName +
                                                           "\" because the access is protected by the (file) system (permission denied).");
                }

                FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            }
            else if (deleteMode == DeleteModes.RecycleBinShowErrors)
            {
                FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            }
            else if (deleteMode == DeleteModes.RecycleBinWithQuestion)
            {
                FileSystem.DeleteFile(file.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            }
            else if (deleteMode == DeleteModes.Direct)
            {
                // Was used for testing the error handling:
                // if (SystemFunctions.random.NextDouble() > 0.5) throw new Exception("Test error");
                file.Delete(ignoreReadOnly: true);
            }
            else
            {
                throw new Exception("Internal error: Unknown delete mode: \"" + deleteMode + "\"");
            }
        }
 public DeleteModeItem(DeleteModes Mode)
 {
     this.DeleteMode = Mode;
 }
 public DeleteModeItem(DeleteModes Mode) => this.DeleteMode = Mode;
        public static void ManuallyDeleteDirectory(string path, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate) return;

            if (path == "") throw new Exception("Could not delete directory because the path was empty.");

            //TODO: Add FileIOPermission code?

            FileSystem.DeleteDirectory(path, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
        }
        public static void SecureDeleteFile(FileInfo file, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate) return;

            if (deleteMode == DeleteModes.RecycleBin)
            {
                // Check CLR permissions -> could raise a exception
                new FileIOPermission(FileIOPermissionAccess.Write, file.FullName).Demand();

                if (IsFileLocked(file))
                    throw new REDPermissionDeniedException("Could not delete file \"" + file.FullName + "\" because the access is protected by the (file) system (permission denied).");

                FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            }
            else if (deleteMode == DeleteModes.RecycleBinShowErrors)
            {
                FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            }
            else if (deleteMode == DeleteModes.RecycleBinWithQuestion) FileSystem.DeleteFile(file.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
            else if (deleteMode == DeleteModes.Direct)
            {
                // Was used for testing the error handling:
                // if (SystemFunctions.random.NextDouble() > 0.5) throw new Exception("Test error");
                file.Delete();
            }
            else throw new Exception("Internal error: Unknown delete mode: \"" + deleteMode.ToString() + "\"");
        }
        public static void SecureDeleteDirectory(string path, DeleteModes deleteMode)
        {
            if (deleteMode == DeleteModes.Simulate) return;

            // Last security check before deletion
            if (Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0)
            {
                if (deleteMode == DeleteModes.RecycleBin)
                {
                    // Check CLR permissions -> could raise a exception
                    new FileIOPermission(FileIOPermissionAccess.Write, path + Path.DirectorySeparatorChar.ToString()).Demand();

                    //if (!CheckWriteAccess(Directory.GetAccessControl(path)))
                    if (IsDirLocked(path))
                        throw new REDPermissionDeniedException("Could not delete directory \"" + path + "\" because the access is protected by the (file) system (permission denied).");

                    FileSystem.DeleteDirectory(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                }
                else if (deleteMode == DeleteModes.RecycleBinShowErrors)
                {
                    FileSystem.DeleteDirectory(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                }
                else if (deleteMode == DeleteModes.RecycleBinWithQuestion) FileSystem.DeleteDirectory(path, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
                else if (deleteMode == DeleteModes.Direct) Directory.Delete(path);
                else throw new Exception("Internal error: Unknown delete mode: \"" + deleteMode.ToString() + "\"");
            }
            else
                throw new Exception("Aborted deletion of the directory \"" + path + "\" because it is no longer empty. This can happen if RED previously failed to delete a empty (trash) file.");
        }