Esempio n. 1
0
        private void secureDelete(String path)
        {
            var emptyDirectory = new DirectoryInfo(path);

            if (!emptyDirectory.Exists)
            {
                throw new Exception("Could not delete the directory \"" + emptyDirectory.FullName + "\" because it does not exist anymore.");
            }

            // Cleanup folder

            // loop trough files and cancel if containsFiles == true
            foreach (var file in emptyDirectory.EnumerateFiles())
            {
                var deleteTrashFile = SystemFunctions.MatchesIgnorePattern(file, ( Int32 )file.Length, this.Data.IgnoreEmptyFiles, this.Data.IgnoreFileList, out var delPattern);

                // If only one file is good, then stop.
                if (deleteTrashFile)
                {
                    try {
                        SystemFunctions.SecureDeleteFile(file, this.Data.DeleteMode);

                        this.Data.AddLogMessage($"-> Successfully deleted file \"{file.FullName}\" because it matched the ignore pattern \"{delPattern}\"");
                    }
                    catch (Exception ex) {
                        this.Data.AddLogMessage($"Failed to delete file \"{file.FullName}\" - Error message: \"{ex.Message}\"");

                        var msg = "Could not delete this empty (trash) file:" + Environment.NewLine + file.FullName + Environment.NewLine + Environment.NewLine +
                                  "Error message: " + ex.Message;

                        if (ex is REDPermissionDeniedException)
                        {
                            throw new REDPermissionDeniedException(msg, ex);
                        }

                        throw new Exception(msg, ex);
                    }
                }
            }


            // End cleanup

            // This function will ensure that the directory is really empty before it gets deleted
            SystemFunctions.SecureDeleteDirectory(emptyDirectory.FullName, this.Data.DeleteMode);
        }