File.Move Does Not Work - File Already Exists

前端 未结 9 1230
无人及你
无人及你 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: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
    }
    

提交回复
热议问题