public static void SafeMoveFile(
            string sourcePath,
            string dstFilePath)
        {
            Trace.TraceInformation(@"About to safe-move file from '{0}' to '{1}'.", sourcePath, dstFilePath);

            if (sourcePath == null || dstFilePath == null)
            {
                Trace.TraceInformation(
                    string.Format(
                        @"Source file path or destination file path does not exist. " +
                        @"Not moving."
                        ));
            }
            else
            {
                if (SafeFileExists(sourcePath))
                {
                    SafeDeleteFile(dstFilePath);

                    var d = ZlpPathHelper.GetDirectoryPathNameFromFilePath(dstFilePath);

                    if (!ZlpIOHelper.DirectoryExists(d))
                    {
                        Trace.TraceInformation(@"Creating non-existing folder '{0}'.", d);
                        ZlpIOHelper.CreateDirectory(d);
                    }

                    ZlpIOHelper.MoveFile(sourcePath, dstFilePath);
                }
                else
                {
                    Trace.TraceInformation(@"Source file path to move does not exist: '{0}'.", sourcePath);
                }
            }
        }
예제 #2
0
 public void MoveTo(ZlpFileInfo destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath.FullName);
 }
예제 #3
0
 public void MoveTo(string destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath);
 }
        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);
            }
        }
예제 #5
0
 public void MoveTo(ZlpFileInfo destinationFilePath)
 => ZlpIOHelper.MoveFile(FullName, destinationFilePath.FullName);
예제 #6
0
 public void MoveTo(string destinationFilePath) => ZlpIOHelper.MoveFile(FullName, destinationFilePath);
예제 #7
0
 public void MoveTo(
     ZlpFileInfo destinationFilePath,
     bool overwriteExisting)
 => ZlpIOHelper.MoveFile(FullName, destinationFilePath.FullName, overwriteExisting);
예제 #8
0
 public void MoveTo(
     string destinationFilePath,
     bool overwriteExisting) => ZlpIOHelper.MoveFile(FullName, destinationFilePath, overwriteExisting);