public static void SafeDeleteFile(
            string filePath)
        {
            Trace.TraceInformation(@"About to safe-delete file '{0}'.", filePath);

            if (!string.IsNullOrEmpty(filePath) &&
                SafeFileExists(filePath))
            {
                try
                {
                    var attributes = ZlpIOHelper.GetFileAttributes(filePath);

                    // Remove read-only attributes.
                    if ((attributes & FileAttributes.Readonly) != 0)
                    {
                        ZlpIOHelper.SetFileAttributes(
                            filePath,
                            attributes & (~(FileAttributes.Readonly)));
                    }

                    ZlpIOHelper.DeleteFile(filePath);
                }
                catch (UnauthorizedAccessException x)
                {
                    var newFilePath =
                        string.Format(
                            @"{0}.{1:N}.deleted",
                            filePath,
                            Guid.NewGuid());

                    Trace.TraceWarning(@"Caught UnauthorizedAccessException while deleting file '{0}'. " +
                                       @"Renaming now to '{1}'. {2}", filePath, newFilePath, x.Message);

                    try
                    {
                        ZlpIOHelper.MoveFile(
                            filePath,
                            newFilePath);
                    }
                    catch (Win32Exception x2)
                    {
                        Trace.TraceWarning(@"Caught IOException while renaming upon failed deleting file '{0}'. " +
                                           @"Renaming now to '{1}'. {2}", filePath, newFilePath, x2.Message);
                    }
                }
                catch (Win32Exception x)
                {
                    var newFilePath =
                        string.Format(
                            @"{0}.{1:N}.deleted",
                            filePath,
                            Guid.NewGuid());

                    Trace.TraceWarning(@"Caught IOException while deleting file '{0}'. " +
                                       @"Renaming now to '{1}'. {2}", filePath, newFilePath, x.Message);

                    try
                    {
                        ZlpIOHelper.MoveFile(
                            filePath,
                            newFilePath);
                    }
                    catch (Win32Exception x2)
                    {
                        Trace.TraceWarning(@"Caught IOException while renaming upon failed deleting file '{0}'. " +
                                           @"Renaming now to '{1}'. {2}", filePath, newFilePath, x2.Message);
                    }
                }
            }
            else
            {
                Trace.TraceInformation(@"Not safe-deleting file '{0}', " +
                                       @"because the file does not exist.", filePath);
            }
        }
Exemplo n.º 2
0
 public void Delete()
 {
     ZlpIOHelper.DeleteFile(_path);
 }
Exemplo n.º 3
0
 public void Delete() => ZlpIOHelper.DeleteFile(FullName);