添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
帅气的斑马  ·  子查询 - Apache Doris·  昨天    · 
追风的打火机  ·  Arm64X PE 文件 | ...·  2 年前    · 
活泼的高山  ·  Java Communications ...·  2 年前    · 
叛逆的警车  ·  eslint的Parsing error: ...·  2 年前    · 
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)
// 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." ); 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"; 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."); 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." 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" 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.") 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 任务

    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)
    // 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." ); 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"; 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."); 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." 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" 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.") 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 任务

  •