File.Move Does Not Work - File Already Exists

前端 未结 9 1130
无人及你
无人及你 2020-12-04 12:08

I\'ve got a folder:

c:\\test

I\'m trying this code:

File.Move(@\"c:\\test\\SomeFile.txt\", @\"c:\\test\\Test\");         


        
相关标签:
9条回答
  • 2020-12-04 12:17

    What you need is:

    if (!File.Exists(@"c:\test\Test\SomeFile.txt")) {
        File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test\SomeFile.txt");
    }
    

    or

    if (File.Exists(@"c:\test\Test\SomeFile.txt")) {
        File.Delete(@"c:\test\Test\SomeFile.txt");
    }
    File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test\SomeFile.txt");
    

    This will either:

    • If the file doesn't exist at the destination location, successfully move the file, or;
    • If the file does exist at the destination location, delete it, then move the file.

    Edit: I should clarify my answer, even though it's the most upvoted! The second parameter of File.Move should be the destination file - not a folder. You are specifying the second parameter as the destination folder, not the destination filename - which is what File.Move requires. So, your second parameter should be c:\test\Test\SomeFile.txt.

    0 讨论(0)
  • 2020-12-04 12:19

    Personally I prefer this method. This will overwrite the file on the destination, removes the source file and also prevent removing the source file when the copy fails.

    string source = @"c:\test\SomeFile.txt";
    string destination = @"c:\test\test\SomeFile.txt";
    
    try
    {
        File.Copy(source, destination, true);
        File.Delete(source);
    }
    catch
    {
        //some error handling
    }
    
    0 讨论(0)
  • 2020-12-04 12:19

    If file really exists and you want to replace it use below code:

    string file = "c:\test\SomeFile.txt"
    string moveTo = "c:\test\test\SomeFile.txt"
    
    if (File.Exists(moveTo))
    {
        File.Delete(moveTo);
    }
    
    File.Move(file, moveTo);
    
    0 讨论(0)
  • 2020-12-04 12:20

    Try Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(Source, Destination, True). The last parameter is Overwrite switch, which System.IO.File.Move doesn't have.

    0 讨论(0)
  • 2020-12-04 12:23

    1) With C# on .Net Core 3.0 and beyond, there is now a third boolean parameter:

    see https://docs.microsoft.com/en-us/dotnet/api/system.io.file.move?view=netcore-3.1

    In .NET Core 3.0 and later versions, you can call Move(String, String, Boolean) setting the parameter overwrite to true, which will replace the file if it exists.
    

    2) For all other versions of .Net, https://stackoverflow.com/a/42224803/887092 is the best answer. Copy with Overwrite, then delete the source file. This is better because it makes it an atomic operation. (I have attempted to update the MS Docs with this)

    0 讨论(0)
  • 2020-12-04 12:27

    You can do a P/Invoke to MoveFileEx() - pass 11 for flags (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
    static extern bool MoveFileEx(string existingFileName, string newFileName, int flags);
    

    Or, you can just call

    Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(existingFileName, newFileName, true);
    

    after adding Microsoft.VisualBasic as a reference.

    0 讨论(0)
提交回复
热议问题