C#文件复制和粘贴
我目前正在使用列表框中的项目(文件名)进行复制和粘贴。没有错误,但复制和粘贴似乎不起作用。我是新来的,所以我不知道这里有什么问题,任何帮助都将不胜感激 代码副本C#文件复制和粘贴,c#,file,clipboard,copy-paste,contextmenustrip,C#,File,Clipboard,Copy Paste,Contextmenustrip,我目前正在使用列表框中的项目(文件名)进行复制和粘贴。没有错误,但复制和粘贴似乎不起作用。我是新来的,所以我不知道这里有什么问题,任何帮助都将不胜感激 代码副本 if(lvwExplorer.SelectedItems[0].Text != "" && lvwExplorer.SelectedItems.Count == 1) { Clipboard.SetText(lvwExplorer.SelectedItems[0].Text);
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);
}
}
在粘贴代码中,仅当目标文件存在时才执行粘贴操作!请更改您的代码:
...
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();
}
...
在粘贴代码中,仅当目标文件存在时才执行粘贴操作!请更改您的代码:
...
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();
}
...
你想干什么?此复制和粘贴是否都在您自己的程序中?粘贴代码是否运行?是,在程序内复制并粘贴。粘贴似乎不起作用请注意,复制操作的
if
语句中的表达式是从左到右处理的。在检查所选项目是否存在之前,您正在尝试访问所选项目。反过来做;-)另外,使用调试器查看剪贴板.SetText(…)是否实际被调用。以同样的方式,使用调试器验证粘贴代码是否正在实际执行。当您在那里使用调试器时,还要检查和验证代码使用的所有变量和结束元素的值。您是否费心使用调试器来找出它不起作用的原因?请使用System.IO.Path来操纵路径,而不是所有那些丑陋的拆分代码。您想做什么?此复制和粘贴是否都在您自己的程序中?粘贴代码是否运行?是,在程序内复制并粘贴。粘贴似乎不起作用请注意,复制操作的if
语句中的表达式是从左到右处理的。在检查所选项目是否存在之前,您正在尝试访问所选项目。反过来做;-)另外,使用调试器查看剪贴板.SetText(…)是否实际被调用。以同样的方式,使用调试器验证粘贴代码是否正在实际执行。当您在那里使用调试器时,还要检查和验证代码使用的所有变量和结束元素的值。您是否费心使用调试器来找出它不起作用的原因?请使用System.IO.Path来操作路径,而不是所有那些丑陋的拆分代码