C# 使用 Directory.Move 时,如果该文件已存在,则无法创建该文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12667770/
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 when using Directory.Move
提问by user1428019
I am trying to move the directory from one location to another location on the same drive. I am getting "Cannot create a file when that file already exists" error. Below is my code.
我正在尝试将目录从一个位置移动到同一驱动器上的另一个位置。我收到“当该文件已存在时无法创建文件”错误。下面是我的代码。
could any one suggest on this?
任何人都可以对此提出建议吗?
string sourcedirectory = @"F:\source";
string destinationdirectory = @"F:\destination";
try
{
if (Directory.Exists(sourcedirectory))
{
if (Directory.Exists(destinationdirectory))
{
Directory.Move(sourcedirectory, destinationdirectory);
}
else
{
Directory.CreateDirectory(destinationdirectory);
Directory.Move(sourcedirectory, destinationdirectory);
}
}
}
catch (Exception ex)
{
log(ex.message);
}
采纳答案by Mark Hall
As both of the previous answers pointed out, the destination Directory cannot exist. In your code you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.
正如前面的两个答案所指出的,目标目录不能存在。在您的代码中,如果目录不存在,则您正在创建目录,然后尝试移动您的目录,移动方法将为您创建目录。如果目录已经存在,您将需要删除它或移动它。
Something like this:
像这样的东西:
class Program
{
static void Main(string[] args)
{
string sourcedirectory = @"C:\source";
string destinationdirectory = @"C:\destination";
string backupdirectory = @"C:\Backup";
try
{
if (Directory.Exists(sourcedirectory))
{
if (Directory.Exists(destinationdirectory))
{
//Directory.Delete(destinationdirectory);
Directory.Move(destinationdirectory, backupdirectory + DateTime.Now.ToString("_MMMdd_yyyy_HHmmss"));
Directory.Move(sourcedirectory, destinationdirectory);
}
else
{
Directory.Move(sourcedirectory, destinationdirectory);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
回答by Vignesh.N
from http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
来自http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
"This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\public\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\public", or specify a new directory name such as "c:\newdir"."
“例如,如果您尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已经存在,则此方法将引发 IOException。您必须指定“c:\public\mydir”作为 destDirName 参数,前提是“c:\public”下不存在“mydir”,或者指定一个新的目录名,例如“c:\newdir”。
回答by cuongle
You don't need to create Directory first, it will throw IO Exception, if destination directory exists, Movemethod automatically creates it for you:
您不需要先创建目录,它会抛出 IO 异常,如果目标目录存在,Move方法会自动为您创建:
string sourcedirectory = @"F:\source";
string destinationdirectory = @"F:\destination";
if (Directory.Exists(sourcedirectory))
{
if (!Directory.Exists(destinationdirectory))
{
Directory.Move(sourcedirectory, destinationdirectory);
}
}
More infomation of Directory.Move:
更多信息Directory.Move:
http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
回答by prashanth
As per MSDN,
根据MSDN,
This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists.
例如,如果您尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已经存在,则此方法将引发 IOException。
But, in your method, you are creating the destination directory before you move.
但是,在您的方法中,您是在移动之前创建目标目录。
So, you need to change your method from
所以,你需要改变你的方法
if (Directory.Exists(destinationdirectory))
{
Directory.Move(sourcedirectory, destinationdirectory);
}
else
{
Directory.CreateDirectory(destinationdirectory);
Directory.Move(sourcedirectory, destinationdirectory);
}
to
到
if (Directory.Exists(destinationdirectory))
{
//delete or rename
}
Directory.Move(sourcedirectory, destinationdirectory);
回答by BornToCode
You can just call
你可以打电话
Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(source, destination, true);
What it does internally is it creates the target directory if it's not exists and then it iterates over the source directory's files and moves them to the target directory. That way the problem of "Cannot create a file when that file already exists" won't happen.
它在内部所做的是,如果目标目录不存在,它会创建目标目录,然后遍历源目录的文件并将它们移动到目标目录。这样就不会发生“当该文件已经存在时无法创建文件”的问题。
You'll need to add Microsoft.VisualBasicas a reference.
您需要添加Microsoft.VisualBasic作为参考。

