将文件从一个地方复制到另一个地方时出错 [英] error while copying file from one place to another

查看:56
本文介绍了将文件从一个地方复制到另一个地方时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从一个地方复制到另一个地方,这是代码.

i am trying to copy a file from one place to another here is the code.

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog2 = new OpenFileDialog();
    openFileDialog2.InitialDirectory = @"C:\";
    openFileDialog2.Title = "Browse Text Files";
    openFileDialog2.CheckFileExists = true;
    openFileDialog2.CheckPathExists = true;

if (openFileDialog2.ShowDialog() == DialogResult.OK)
   {
       textBox2.Text = openFileDialog2.FileName;
   }
string filename,filex;
   filex = openFileDialog2.SafeFileName;

   filename = textBox2.Text;
   string dpath = @"D:\G\"+ filex ;
   File.Copy(filename, dpath );


但这给了我以下错误
文件``D:\ G \ openFileDialog2''已经存在.
请告诉我代码有什么问题.


but it gives me the follwoing error
The file ''D:\G\openFileDialog2'' already exists.
please tell me whats wrong with the code.

推荐答案

该消息使用非常简单的英语-该位置的文件已经存在.如果要覆盖文件,请使用重载的方法 File.Copy方法(字符串,字符串,布尔值) [^ ].将true传递给overwrite参数,它应该很好.
The message is in a very simple English - the file at that location already exist. If you want to overwrite the file, use the overloaded method File.Copy Method (String, String, Boolean)[^]. Pass true to the overwrite parameter and it should be good.


这篇关于将文件从一个地方复制到另一个地方时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆