File.Move Does Not Work - File Already Exists

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

提交回复
热议问题