C# 无法使用file.Move创建已存在的文件

C# 无法使用file.Move创建已存在的文件,c#,C#,我试图将一个文件从我的Resx移动到我的PC,但我一直有问题 因此,我在参考资料中导入了一个名为“bad”的文件夹,并使用File.Move方法将文件夹“bad”移动到我的电脑中。 但是程序不断崩溃,因为它说:已经存在的文件无法创建。 下面是我使用的代码: //txtpath is the root folder. I let the user choose the root folder and save it in txtpath.text private void btnbadname_

我试图将一个文件从我的Resx移动到我的PC,但我一直有问题

因此,我在参考资料中导入了一个名为“bad”的文件夹,并使用
File.Move
方法将文件夹“bad”移动到我的电脑中。 但是程序不断崩溃,因为它说:
已经存在的文件无法创建。

下面是我使用的代码:

//txtpath is the root folder. I let the user choose the root folder and save it in txtpath.text

private void btnbadname_Click(object sender, EventArgs e)
{
        string source = "Resources\bad";
        string destination = txtpath.Text + @"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App";

        File.Move(source, destination);

        MessageBox.Show("脏话ID已开启, 教程请点击下面的链接");
}

您正在使用
File.Move
移动目录,为什么不使用
directory.Move

仅将文件从源移动到目标,而
目录。move
将移动目录本身

如果我误解了你,你想移动一个文件

您可以在使用以下内容之前检查文件是否存在:

if(File.Exists(fileName))
    File.Delete(fileName);
//Set the location of your directories                
string sourceDirectory = @"";
string destDirectory = @"";

//Check if the directory exists, and if not create it
if (!Directory.Exists(destDirectory))
    Directory.CreateDirectory(destDirectory);

DirectoryInfo sourceDirInfo = new DirectoryInfo(sourceDirectory);
//Iterate through directory and check the existance of each file
foreach (FileInfo sourceFileInfo in sourceDirInfo.GetFiles())
{
    string fileName = sourceFileInfo.Name;
    string destFile = Path.Combine(destDirectory, fileName);
    if (File.Exists(destFile))
        File.Delete(destFile);

    //Finally move the file    
    File.Move(sourceFileInfo.FullName, destFile);
}
编辑: 如果要遍历目录并在移动文件之前确保该文件不存在,可以使用以下方法:

if(File.Exists(fileName))
    File.Delete(fileName);
//Set the location of your directories                
string sourceDirectory = @"";
string destDirectory = @"";

//Check if the directory exists, and if not create it
if (!Directory.Exists(destDirectory))
    Directory.CreateDirectory(destDirectory);

DirectoryInfo sourceDirInfo = new DirectoryInfo(sourceDirectory);
//Iterate through directory and check the existance of each file
foreach (FileInfo sourceFileInfo in sourceDirInfo.GetFiles())
{
    string fileName = sourceFileInfo.Name;
    string destFile = Path.Combine(destDirectory, fileName);
    if (File.Exists(destFile))
        File.Delete(destFile);

    //Finally move the file    
    File.Move(sourceFileInfo.FullName, destFile);
}

目标目录不能存在。在代码中,如果目录不存在,则创建目录,然后尝试移动目录,move方法将为您创建目录。如果该目录已存在,则需要将其删除或移动

见:

目的地也应该有文件名

string destination = txtpath.Text + @"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App\yourfilename.ext";

使用MoveTo时,请提供发送文件的完整路径,包括文件名,例如pic123.jpg。如果使用DirectoryInfo获取文件数组并希望移动其中任何文件,请将文件的Name属性附加到发送文件的目录路径。
imgFile.MoveTo(“C:\myPictures\ArchiveFolder\pic123.jpg”)

我认为您需要为
file.Move方法指定文件名的完整路径。您的
源文件
根本不是具有完整路径的文件名。如果你有
无法创建一个已经存在的文件
错误,我认为这个错误来自其他地方。.为什么没有人使用路径连接?如果你想移动文件夹,可能会重复为什么你要使用
文件。移动
,使用
目录如何。移动
你肯定你的“释放”了吗dir后面有空格
releases
?我想我明白你的意思了,谢谢。有没有什么方法可以让我将文件移动到目的地,如果已经存在,还可以替换?请尝试使用目录。按贝多的话移动。我可以问另一个问题吗?如果我使用的文件来自Resources@user3383530是否要在移动文件之前创建目录?如果是这样,那么您可以使用
Directory.Exists(directoryName)
,如果该目录不存在,则使用
Directory.CreateDirectory(directoryName)
创建该目录。嗯,您有skype或facebook吗?如果我能用即时聊天lol问你问题会更好。。我现在想做的是,如果用户点击按钮,程序将用我的电脑中的文件替换用户电脑中已经存在的一些文件Resources@rain,我已停用我的fb帐户,让我检查激活情况