public void MoveTo(string destDirName)
{
if (destDirName == null)
{
throw new ArgumentNullException(nameof(destDirName));
}
if (destDirName.Length == 0)
{
throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destDirName));
}
string destination = Path.GetFullPath(destDirName);
string destinationWithSeparator = destination;
if (destinationWithSeparator[destinationWithSeparator.Length - 1] != Path.DirectorySeparatorChar)
{
destinationWithSeparator = destinationWithSeparator + PathHelpers.DirectorySeparatorCharAsString;
}
string fullSourcePath;
if (FullPath.Length > 0 && FullPath[FullPath.Length - 1] == Path.DirectorySeparatorChar)
{
fullSourcePath = FullPath;
}
else
{
fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString;
}
StringComparison pathComparison = PathInternal.StringComparison;
if (string.Equals(fullSourcePath, destinationWithSeparator, pathComparison))
{
throw new IOException(SR.IO_SourceDestMustBeDifferent);
}
string sourceRoot = Path.GetPathRoot(fullSourcePath);
string destinationRoot = Path.GetPathRoot(destinationWithSeparator);
if (!string.Equals(sourceRoot, destinationRoot, pathComparison))
{
throw new IOException(SR.IO_SourceDestMustHaveSameRoot);
}
// Windows will throw if the source file/directory doesn't exist, we preemptively check
// to make sure our cross platform behavior matches NetFX behavior.
if (!Exists && !FileSystem.FileExists(FullPath))
{
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullPath));
}
if (FileSystem.DirectoryExists(destinationWithSeparator))
{
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destinationWithSeparator));
}
FileSystem.MoveDirectory(FullPath, destination);
Init(originalPath: destDirName,
fullPath: destinationWithSeparator,
fileName: _name,
isNormalized: true);
// Flush any cached information about the directory.
Invalidate();
}