Skip to content

Commit

Permalink
Changed Directory.Move to throw IOException instead of UnauthorizedAc…
Browse files Browse the repository at this point in the history
…cessException to be more similar to System.IO.Directory.Move
  • Loading branch information
Peter Ritchie committed Dec 21, 2014
1 parent 62ba199 commit 8b6011e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Pri.LongPath/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,15 @@ private static bool IsCurrentOrParentDirectory(string directoryName)

public static void Move(string sourcePath, string destinationPath)
{
File.Move(sourcePath, destinationPath);
string normalizedSourcePath = Path.NormalizeLongPath(sourcePath, "sourcePath");
string normalizedDestinationPath = Path.NormalizeLongPath(destinationPath, "destinationPath");

if (NativeMethods.MoveFile(normalizedSourcePath, normalizedDestinationPath)) return;

var lastWin32Error = Marshal.GetLastWin32Error();
if(lastWin32Error == NativeMethods.ERROR_ACCESS_DENIED)
throw new System.IO.IOException(string.Format("Access to the path '{0}'is denied.", sourcePath), NativeMethods.MakeHRFromErrorCode(lastWin32Error));
throw Common.GetExceptionFromWin32Error(lastWin32Error, "path");
}

/// <summary>
Expand Down
61 changes: 61 additions & 0 deletions Tests/DirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,67 @@ public void TestMove()
Directory.Delete(tempLongPathFilename2, recursive);
}

[TestMethod, ExpectedException(typeof(IOException))]
public void TestInUseMove()
{
const bool recursive = true;

#if SHORT_SOURCE
var tempPathFilename1 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
System.IO.Directory.CreateDirectory(tempPathFilename1);
Assert.IsTrue(System.IO.Directory.Exists(Path.GetFullPath(tempPathFilename1)));
var tempPathFilename2 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
System.IO.Directory.CreateDirectory(tempPathFilename2);
Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
try
{
using (
var writer = System.IO.File.CreateText(System.IO.Path.Combine(tempPathFilename2, "TestInUseMove")))
{
string destinationPath =
System.IO.Path.GetFullPath(System.IO.Path.Combine(tempPathFilename1, System.IO.Path.GetFileName(tempPathFilename2)));
System.IO.Directory.Move(tempPathFilename2, destinationPath);
Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename1)));
Assert.IsFalse(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
Assert.IsTrue(System.IO.Directory.Exists(destinationPath));
}
}
catch (Exception e)
{
throw;
}
finally
{
Directory.Delete(tempPathFilename1, recursive);
Directory.Delete(tempPathFilename2, recursive);
}
#endif
var tempLongPathFilename1 = Path.Combine(longPathDirectory, Path.GetRandomFileName());
Directory.CreateDirectory(tempLongPathFilename1);
Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
var tempLongPathFilename2 = Path.Combine(longPathDirectory, Path.GetRandomFileName());
Directory.CreateDirectory(tempLongPathFilename2);
Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
try
{
using (
var writer = File.CreateText(Path.Combine(tempLongPathFilename2, "TestInUseMove")))
{
string destinationPath =
Path.GetFullPath(Path.Combine(tempLongPathFilename1, Path.GetFileName(tempLongPathFilename2)));
Directory.Move(tempLongPathFilename2, destinationPath);
Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
Assert.IsTrue(Directory.Exists(destinationPath));
}
}
finally
{
Directory.Delete(tempLongPathFilename1, recursive);
Directory.Delete(tempLongPathFilename2, recursive);
}
}

[TestMethod]
public void TestGetAccessControl()
{
Expand Down

0 comments on commit 8b6011e

Please sign in to comment.