SIL.Core/IO/FileUtils.cs
/// <summary>
/// If there is a problem doing the replace, a dialog is shown which tells the user
/// what happened, and lets them try to fix it.  It also lets them "Give up", in
/// which case this returns False.
///
/// To help with situations where something may temporarily be holding on to the file,
/// this will retry for up to 5 seconds.
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="destinationPath"></param>
/// <param name="backupPath">can be null if you don't want a replacement</param>
/// <returns>if the user gives up, throws whatever exception the file system gave</returns>
If there is a problem doing the replace, a dialog is shown which tells the user
what happened, and lets them try to fix it.  It also lets them "Give up", in
which case this returns False.

To help with situations where something may temporarily be holding on to the file,
this will retry for up to 5 seconds.
If there is a problem doing the replace a dialog is shown which tells the user what happened and lets them try to fix it . It also lets them " Give up " in which case this returns False . To help with situations where something may temporarily be holding on to the file this will retry for up to 5 seconds .
public static void ReplaceFileWithUserInteractionIfNeeded(string sourcePath,
																  string destinationPath,
																  string backupPath)
		{
			bool succeeded = false;
			do
			{
				try
				{
					if (UnsafeForFileReplaceMethod(sourcePath) || UnsafeForFileReplaceMethod(destinationPath)
						||
						(!string.IsNullOrEmpty(backupPath) && !PathUtilities.PathsAreOnSameVolume(sourcePath,backupPath))
						||
						!PathUtilities.PathsAreOnSameVolume(sourcePath, destinationPath)
						||
						!File.Exists(destinationPath)
						)
					{
						if (!string.IsNullOrEmpty(backupPath) && File.Exists(destinationPath))
						{
							File.Copy(destinationPath, backupPath, true);
						}
						File.Copy(sourcePath, destinationPath, true);
						File.Delete(sourcePath);
					}
					else
					{
						var giveUpTime = DateTime.Now.AddSeconds(5);
						Exception theProblem;
						do
						{
							try
							{
								theProblem = null;
								File.Replace(sourcePath, destinationPath, backupPath);
							}
							catch (Exception e)
							{
								theProblem = e;
								Thread.Sleep(100);
							}
						} while (theProblem != null && DateTime.Now < giveUpTime);
						if (theProblem != null)
							throw theProblem;
					}
					succeeded = true;
				}
				catch (UnauthorizedAccessException error)
				{
					ReportFailedReplacement(destinationPath, error);
				}
				catch (IOException error)
				{
					ReportFailedReplacement(destinationPath, error);
				}
			}
			while (!succeeded);
		}
public static void ReplaceFileWithUserInteractionIfNeeded ( string sourcePath , string destinationPath , string backupPath ) { bool succeeded = false ; do { try { if ( UnsafeForFileReplaceMethod ( sourcePath ) || UnsafeForFileReplaceMethod ( destinationPath ) || ( ! string . IsNullOrEmpty ( backupPath ) && ! PathUtilities . PathsAreOnSameVolume ( sourcePath , backupPath ) ) || ! PathUtilities . PathsAreOnSameVolume ( sourcePath , destinationPath ) || ! File . Exists ( destinationPath ) ) { if ( ! string . IsNullOrEmpty ( backupPath ) && File . Exists ( destinationPath ) ) { File . Copy ( destinationPath , backupPath , true ) ; } File . Copy ( sourcePath , destinationPath , true ) ; File . Delete ( sourcePath ) ; } else { var giveUpTime = DateTime . Now . AddSeconds ( 5 ) ; Exception theProblem ; do { try { theProblem = null ; File . Replace ( sourcePath , destinationPath , backupPath ) ; } catch ( Exception e ) { theProblem = e ; Thread . Sleep ( 100 ) ; } } while ( theProblem != null && DateTime . Now < giveUpTime ) ; if ( theProblem != null ) throw theProblem ; } succeeded = true ; } catch ( UnauthorizedAccessException error ) { ReportFailedReplacement ( destinationPath , error ) ; } catch ( IOException error ) { ReportFailedReplacement ( destinationPath , error ) ; } } while ( ! succeeded ) ; }
If there is a problem doing the replace, a dialog is shown which tells the user
what happened, and lets them try to fix it.
If there is a problem doing the replace a dialog is shown which tells the user what happened and lets them try to fix it .
//can't use File.Replace or File.Move across volumes (sigh)
if the user gives up, throws whatever exception the file system gave
if the user gives up throws whatever exception the file system gave
sourcePath
string
None
destinationPath
string
None
backupPath
string
can be null if you don't want a replacement
can be null if you don ' t want a replacement