我目前正在使用“列表框中的带有项目(文件名)的复制和粘贴”。没有错误,但复制和粘贴似乎无效。我是新手,所以我不知道这里出了什么问题,我们将不胜感激。
复制代码
if(lvwExplorer.SelectedItems[0].Text != "" && lvwExplorer.SelectedItems.Count == 1)
{
Clipboard.SetText(lvwExplorer.SelectedItems[0].Text);
}
else
{
MessageBox.Show("You can only copy one element at a time.", "Cannot Copy", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
粘贴代码
string path = Clipboard.GetText();
char seperator = '\\';
string originalFileName = path.Split(seperator)[path.Split(seperator).Length - 1];
string target = cbxAddress.Text + "\\" + originalFileName;
try
{
if(File.Exists(target))
{
if (MessageBox.Show("The File you want to copy already exists. Do you want to replace it?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
File.Delete(target);
File.Copy(path, target, false);
GoToDirectory();
}
}
}
catch(Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
答案 0 :(得分:3)
在粘贴代码中,仅当目标文件存在时才执行粘贴操作!请更改您的代码:
...
if(File.Exists(target))
{
if (MessageBox.Show("The File you want to copy already exists. Do you want to replace it?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
File.Delete(target);
File.Copy(path, target, false);
GoToDirectory();
}
}
else
{
File.Copy(path, target, false);
GoToDirectory();
}
...