File.Move不起作用 - 文件已经存在 [英] File.Move Does Not Work - File Already Exists
本文介绍了File.Move不起作用 - 文件已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件夹:
c:\ test
我试着这段代码:
File.Move(@c:\\ \\ test \SomeFile.txt,@c:\test\Test);
我得到异常:
文件已经存在
输出目录肯定存在,输入文件就在那里。你需要把它移动到另一个文件(而不是文件夹),这也可以用来重命名。
Move:
File.Move(@c:\test\SomeFile.txt, @ C:\test\Test\SomeFile.txt);
重命名:
File.Move(@c:\test\SomeFile.txt,@c:\test\SomeFile2.txt);
在您的示例中,File already exists的原因是因为 C:\test\Test
试图创建一个没有扩展名的文件 Test
,但是不能这样做,因为一个文件夹已经存在名字。
I've got a folder:
c:\test
I'm trying this code:
File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test");
I get exception:
File already exists
The output directory definitely exists and the input file is there.
解决方案
You need to move it to another file (rather than a folder), this can also be used to rename.
Move:
File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test\SomeFile.txt");
Rename:
File.Move(@"c:\test\SomeFile.txt", @"c:\test\SomeFile2.txt");
The reason it says "File already exists" in your example, is because C:\test\Test
tries to create a file Test
without an extension, but cannot do so as a folder already exists with the same name.
这篇关于File.Move不起作用 - 文件已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文