File.Move 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Move(String, String) |
将指定文件移到新位置,提供要指定新文件名的选项。 |
Move(String, String, Boolean) |
将指定文件移动到新位置,提供用于指定新文件名和替换目标文件(如果已存在)的选项。 |
Move(String, String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
将指定文件移到新位置,提供要指定新文件名的选项。
public:
static void Move(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Move (string sourceFileName, string destFileName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String)
参数
- sourceFileName
- String
要移动的文件的名称。 可以包括相对或绝对路径。
- destFileName
- String
文件的新路径和名称。
例外
未找到 sourceFileName
。
sourceFileName
或 destFileName
为 null
。
.NET Framework和 .NET Core 版本早于 2.1:sourceFileName
或 destFileName
是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
调用方没有所要求的权限。
指定的路径和/或文件名超过了系统定义的最大长度。
sourceFileName
或 destFileName
中指定的路径无效(例如,它位于未映射的驱动器上)。
sourceFileName
或 destFileName
的格式无效。
示例
以下示例移动文件。
using namespace System;
using namespace System::IO;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
String^ path2 = "c:\\temp2\\MyTest.txt";
try
{
if ( !File::Exists( path ) )
{
// This statement ensures that the file is created,
// but the handle is not kept.
FileStream^ fs = File::Create( path );
if ( fs )
delete (IDisposable^)fs;
}
// Ensure that the target does not exist.
if ( File::Exists( path2 ) )
File::Delete( path2 );
// Move the file.
File::Move( path, path2 );
Console::WriteLine( "{0} was moved to {1}.", path, path2 );
// See if the original exists now.
if ( File::Exists( path ) )
{
Console::WriteLine( "The original file still exists, which is unexpected." );
}
else
{
Console::WriteLine( "The original file no longer exists, which is expected." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp2\MyTest.txt";
try
{
if (!File.Exists(path))
{
// This statement ensures that the file is created,
// but the handle is not kept.
using (FileStream fs = File.Create(path)) {}
}
// Ensure that the target does not exist.
if (File.Exists(path2))
File.Delete(path2);
// Move the file.
File.Move(path, path2);
Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now.
if (File.Exists(path))
{
Console.WriteLine("The original file still exists, which is unexpected.");
}
else
{
Console.WriteLine("The original file no longer exists, which is expected.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System.IO
let path = @"c:\temp\MyTest.txt"
let path2 = @"c:\temp2\MyTest.txt"
if File.Exists path |> not then
// This statement ensures that the file is created,
// but the handle is not kept.
use _ = File.Create path
()
// Ensure that the target does not exist.
if File.Exists path2 then
File.Delete path2
// Move the file.
File.Move(path, path2)
printfn $"{path} was moved to {path2}."
// See if the original exists now.
if File.Exists path then
printfn "The original file still exists, which is unexpected."
else
printfn "The original file no longer exists, which is expected."
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp2\MyTest.txt"
Try
If File.Exists(path) = False Then
' This statement ensures that the file is created,
' but the handle is not kept.
Dim fs As FileStream = File.Create(path)
fs.Close()
End If
' Ensure that the target does not exist.
If File.Exists(path2) Then
File.Delete(path2)
End If
' Move the file.
File.Move(path, path2)
Console.WriteLine("{0} moved to {1}", path, path2)
' See if the original file exists now.
If File.Exists(path) Then
Console.WriteLine("The original file still exists, which is unexpected.")
Else
Console.WriteLine("The original file no longer exists, which is expected.")
End If
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
此方法适用于磁盘卷,如果源和目标相同,则不会引发异常。
请注意,如果尝试通过将同名文件移动到该目录中来替换文件, IOException 则会引发 。 避免此问题的方法:
在 .NET Core 3.0 及更高版本中,可以调用 Move(String, String, Boolean) 将 参数
overwrite
true
设置为 ,这将替换文件(如果存在)。在所有 .NET 版本中,可以调用 Copy(String, String, Boolean) 以使用 overwrite 进行复制,然后调用
Delete
以删除多余的源文件。 如果要复制的文件较小,并且你正在寻找“原子”文件操作,则建议使用此策略。Delete
如果文件优先,并且系统或程序崩溃,则目标文件将不再存在。在所有 .NET 版本中,可以在调用
Move
之前调用 Delete(String) ,这只会删除文件(如果存在)。
sourceFileName
和 destFileName
参数可以包含相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
跨磁盘卷移动文件等效于复制文件并从源中删除文件(如果复制成功)。
如果尝试跨磁盘卷移动文件,并且该文件正在使用中,则会将该文件复制到目标,但不会从源中删除该文件。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Move(String, String, Boolean)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
将指定文件移动到新位置,提供用于指定新文件名和替换目标文件(如果已存在)的选项。
public:
static void Move(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Move (string sourceFileName, string destFileName, bool overwrite);
static member Move : string * string * bool -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String, overwrite As Boolean)
参数
- sourceFileName
- String
要移动的文件的名称。 可以包括相对或绝对路径。
- destFileName
- String
文件的新路径和名称。
- overwrite
- Boolean
true
替换目标文件(如果已存在); false
否则。
例外
未找到 sourceFileName
。
sourceFileName
或 destFileName
为 null
。
.NET Framework和 .NET Core 版本早于 2.1:sourceFileName
或 destFileName
是长度为零的字符串,仅包含空格或包含无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
指定的路径和/或文件名超过了系统定义的最大长度。
sourceFileName
或 destFileName
中指定的路径无效(例如,它位于未映射的驱动器上)。
sourceFileName
或 destFileName
的格式无效。
示例
以下示例移动文件。
using namespace System;
using namespace System::IO;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
String^ path2 = "c:\\temp2\\MyTest.txt";
try
{
if ( !File::Exists( path ) )
{
// This statement ensures that the file is created,
// but the handle is not kept.
FileStream^ fs = File::Create( path );
if ( fs )
delete (IDisposable^)fs;
}
// Ensure that the target does not exist.
if ( File::Exists( path2 ) )
File::Delete( path2 );
// Move the file.
File::Move( path, path2 );
Console::WriteLine( "{0} was moved to {1}.", path, path2 );
// See if the original exists now.
if ( File::Exists( path ) )
{
Console::WriteLine( "The original file still exists, which is unexpected." );
}
else
{
Console::WriteLine( "The original file no longer exists, which is expected." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp2\MyTest.txt";
try
{
if (!File.Exists(path))
{
// This statement ensures that the file is created,
// but the handle is not kept.
using (FileStream fs = File.Create(path)) {}
}
// Ensure that the target does not exist.
if (File.Exists(path2))
File.Delete(path2);
// Move the file.
File.Move(path, path2);
Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now.
if (File.Exists(path))
{
Console.WriteLine("The original file still exists, which is unexpected.");
}
else
{
Console.WriteLine("The original file no longer exists, which is expected.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System.IO
let path = @"c:\temp\MyTest.txt"
let path2 = @"c:\temp2\MyTest.txt"
if File.Exists path |> not then
// This statement ensures that the file is created,
// but the handle is not kept.
use _ = File.Create path
()
// Ensure that the target does not exist.
if File.Exists path2 then
File.Delete path2
// Move the file.
File.Move(path, path2)
printfn $"{path} was moved to {path2}."
// See if the original exists now.
if File.Exists path then
printfn "The original file still exists, which is unexpected."
else
printfn "The original file no longer exists, which is expected."
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp2\MyTest.txt"
Try
If File.Exists(path) = False Then
' This statement ensures that the file is created,
' but the handle is not kept.
Dim fs As FileStream = File.Create(path)
fs.Close()
End If
' Ensure that the target does not exist.
If File.Exists(path2) Then
File.Delete(path2)
End If
' Move the file.
File.Move(path, path2)
Console.WriteLine("{0} moved to {1}", path, path2)
' See if the original file exists now.
If File.Exists(path) Then
Console.WriteLine("The original file still exists, which is unexpected.")
Else
Console.WriteLine("The original file no longer exists, which is expected.")
End If
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
此方法适用于磁盘卷,如果源和目标相同,则不会引发异常。
sourceFileName
和 destFileName
参数可以包含相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
跨磁盘卷移动文件等效于复制文件并从源中删除文件(如果复制成功)。
如果尝试跨磁盘卷移动文件,并且该文件正在使用中,则会将该文件复制到目标,但不会从源中删除该文件。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。