Howto safely move a file using C#
If possible this version uses the special File.Replace on NTFS and graciously falls back on Delete+Move on any other file system. Nothing else to comment about here. I am just amazed that this kind of save moving is not supported by the framework itself. Instead the framework's move routine throws an exception if there is an file existing at the target location. Weird. ![]()
Just a heads up: Don't use this in performance critical parts...
C#:
- using System;
- using System.IO;
- namespace Com.Hertkorn.Helper.Filesystem
- {
- public static class FileMover
- {
- /// <summary>
- /// Securely moves a file to a new location. Overwrites any
- /// preexisting file at new location (= replacing file).
- /// </summary>
- /// <remarks>
- /// If NTFS is available this is done via File.Replace.
- /// If NTFS is not available it will be moved via deleting
- /// any preexisting file and moving. Do NOT rely on the
- /// backupFile being there - or not - after the move process.
- /// That is not predetermined. This method is clearly
- /// optimized for the case that NTFS is available. Consider NOT
- /// using it on any other filesystem, if performance is an issue!
- /// </remarks>
- /// <param name="sourceLocation">The file to be
- /// moved.</param>
- /// <param name="targetLocation">The new resting
- /// place of the file.</param>
- /// <param name="backupLocation">A backup location that is
- /// used when replacing on NTFS.</param>
- public static void FileMove(
- FileInfo sourceLocation,
- FileInfo targetLocation,
- FileInfo backupLocation)
- {
- if (targetLocation.Exists)
- {
- try
- {
- File.Replace(
- sourceLocation.FullName,
- targetLocation.FullName,
- backupLocation.FullName, true);
- }
- catch (PlatformNotSupportedException)
- {
- // Not operating on an NTFS volume
- if (targetLocation.Exists)
- {
- targetLocation.Delete();
- }
- File.Move(sourceLocation.FullName, targetLocation.FullName);
- }
- }
- else
- {
- File.Move(sourceLocation.FullName, targetLocation.FullName);
- }
- }
- }
- }

Nice. You might want to spell check a little, tho.
Comment on March 3, 2008 @ 17:14:53
Thanks for the heads-up.
Comment on March 3, 2008 @ 18:47:51
Hi….Can I move file between two servers? Thank you!!!
Comment on January 9, 2009 @ 00:34:13
Hi!
As far as I understand the MS documentation for File.Replace and Kernel32::ReplaceFile, File.Replace() works on all windows OSs, except Win9x.
The call to File.Replace does not require to have NTFS.
Also see the source code of File.cs:
#if !FEATURE_PAL
// Win9x doesn’t support ReplaceFile
if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
throw new PlatformNotSupportedException(Environment.GetResourceString(“PlatformNotSupported_Win9x”));
#endif
int flags = Win32Native.REPLACEFILE_WRITE_THROUGH;
if (ignoreMetadataErrors)
flags |= Win32Native.REPLACEFILE_IGNORE_MERGE_ERRORS;
bool r = Win32Native.ReplaceFile(fullDestPath, fullSrcPath, fullBackupPath, flags, IntPtr.Zero, IntPtr.Zero);
Comment on May 14, 2009 @ 12:36:31
i have here codes..i want to move the file after it was created on the debug folder..i can’t think of a way..please do help..
txtfile1.txt contains the filename of my program..how can i create a code where
DirectoryInfo source = new DirectoryInfo(txtFile1.Text + “xyz.txt”);
File.Move(source.ToString(), @”C:\\Wiimote Graph Points\\”);
Comment on March 3, 2010 @ 16:57:47