Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#文件复制和粘贴_C#_File_Clipboard_Copy Paste_Contextmenustrip - Fatal编程技术网

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来操作路径,而不是所有那些丑陋的拆分代码