C# 该文件已存在时无法创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1679022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot create a file when that file already exists
提问by Ramesh
I am using Winforms, and I am trying to copy a file from one location to another. If a file with the same name is already there, I want to overwrite it. But I get an error like "Cannot create a file when that file already exists."
我正在使用 Winforms,并且正在尝试将文件从一个位置复制到另一个位置。如果同名文件已经存在,我想覆盖它。但我收到类似“当该文件已存在时无法创建文件”的错误消息。
I want to overwrite the file. What should I do?
I tried File.copy
instead of File.move
, but I got the same error.
我想覆盖文件。我该怎么办?我尝试了File.copy
而不是File.move
,但我遇到了同样的错误。
回答by Vinko Vrsalovic
回答by sdp07
have you tried File.Copy(src, dest, true)
. This might help overwriting the existing file.
你试过吗File.Copy(src, dest, true)
。这可能有助于覆盖现有文件。
回答by Himadri
Check the write permission is allowed for the folder contains the destination file.
检查包含目标文件的文件夹是否允许写入权限。
Try the following:
请尝试以下操作:
System.IO.File.Copy(src, dst, true);
System.IO.File.Copy(src, dst, true);
true if you want the existing file will be overwritten.
如果您希望现有文件将被覆盖,则为 true。
To change or set file permission click here
要更改或设置文件权限,请单击此处
回答by Jerry
I had the same error. The destination should be the new filename, not the destination folder.
我有同样的错误。目标应该是新文件名,而不是目标文件夹。