Directory.Move(String, String) 方法
定义
将文件或目录及其内容移到新位置。Moves a file or a directory and its contents to a new location.
public:
static void Move(System::String ^ sourceDirName, System::String ^ destDirName);
public static void Move (string sourceDirName, string destDirName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceDirName As String, destDirName As String)
参数
- sourceDirName
- String
要移动的文件或目录的路径。The path of the file or directory to move.
- destDirName
- String
sourceDirName 的新位置的路径。The path to the new location for sourceDirName. 如果 sourceDirName 是文件,那么 destDirName 也必须是文件名。If sourceDirName is a file, then destDirName must also be a file name.
例外
尝试将目录移动到不同的卷。An attempt was made to move a directory to a different volume.
- 或 --or-
destDirName 已存在。destDirName already exists. 请参阅“备注”部分中的“说明”。See the Note in the Remarks section.
- 或 --or-
sourceDirName 和 destDirName 参数引用同一个文件或目录。The sourceDirName and destDirName parameters refer to the same file or directory.
- 或 --or- 另一个进程正在使用目录或其中一个文件。The directory or a file within it is being used by another process.
调用方没有所要求的权限。The caller does not have the required permission.
sourceDirName 或 destDirName 是一个长度为零的字符串,仅包含空格,或者包含一个或多个无效字符。sourceDirName or destDirName is a zero-length string, contains only white space, or contains one or more invalid characters. 你可以使用 GetInvalidPathChars() 方法查询无效字符。You can query for invalid characters with the GetInvalidPathChars() method.
sourceDirName 或 destDirName 为 null。sourceDirName or destDirName is null.
指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.
sourceDirName 指定的路径无效(例如,它位于未映射的驱动器上)。The path specified by sourceDirName is invalid (for example, it is on an unmapped drive).
示例
下面的示例演示如何将目录及其所有文件移动到新目录。The following example demonstrates how to move a directory and all its files to a new directory. 移动原始目录后,原始目录将不再存在。The original directory no longer exists after it has been moved.
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\source";
string destinationDirectory = @"C:\destination";
try
{
Directory.Move(sourceDirectory, destinationDirectory);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Imports System.IO
Module Module1
Sub Main()
Dim sourceDirectory As String = "C:\source"
Dim destinationDirectory As String = "C:\destination"
Try
Directory.Move(sourceDirectory, destinationDirectory)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
End Module
注解
此方法使用指定的名称创建一个新目录 destDirName ,并将的内容移动 sourceDirName 到新创建的目标目录。This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory. 如果尝试将目录移动到已存在的目录,则 IOException 会发生。If you try to move a directory to a directory that already exists, an IOException will occur. 例如,如果尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已存在,则会出现异常。For example, an exception will occur if you try to move c:\mydir to c:\public, and c:\public already exists. 或者,您可以将 "c: \ \ 公用 \ \mydir" 指定为 destDirName 参数,前提是 "mydir" 在 "c: \ 公用" 下不存在 \ ,或指定新目录名称,如 "c: \ \newdir"。Alternatively, you could specify "c:\\public\\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\\public", or specify a new directory name such as "c:\\newdir".
sourceDirName允许和 destDirName 参数指定相对或绝对路径信息。The sourceDirName and destDirName arguments are permitted to specify relative or absolute path information. 相对路径信息被解释为相对于当前工作目录。Relative path information is interpreted as relative to the current working directory. 若要获取当前工作目录,请参见 GetCurrentDirectory 。To obtain the current working directory, see GetCurrentDirectory.
移动目录之前,会从路径参数的末尾删除尾随空格。Trailing spaces are removed from the end of the path parameters before moving the directory.
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。For a list of common I/O tasks, see Common I/O Tasks.
备注
从 .NET Core 3.0 开始, Move 如果已存在,则方法会 IOException 在所有平台中引发 destDirName 。Starting with .NET Core 3.0, the Move method throws an IOException in all platforms when the destDirName already exists. 在 .NET Core 2.2 和早期版本中,仅在 Windows 上引发异常,其他平台可能会失败或覆盖 destDirName 。In .NET Core 2.2 and previous versions, the exception was only thrown on Windows, and other platforms could either fail or overwrite the destDirName. 请参阅 c + + 重命名。See C++ rename.