C# 拖放复制文件

C# 拖放复制文件,c#,.net,windows,winforms,forms,C#,.net,Windows,Winforms,Forms,我可能做了一些有点愚蠢的事,但看不出是什么 string pegasusKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Pegasus\"; string opera2ServerPath = @"Server VFP\"; string opera3ServerPath = @"O3 Client VFP\"; string opera2InstallationPath = null; string ope

我可能做了一些有点愚蠢的事,但看不出是什么

string pegasusKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Pegasus\";
        string opera2ServerPath = @"Server VFP\";
        string opera3ServerPath = @"O3 Client VFP\";
        string opera2InstallationPath = null;
        string opera3InstallationPath = null;

        //Gets the opera Installtion paths and reads to the string opera*InstallationPath
        opera2InstallationPath = (string)Registry.GetValue(pegasusKey + opera2ServerPath +    "System", "PathToServerDynamic", null);
        opera3InstallationPath = (string)Registry.GetValue(pegasusKey + opera3ServerPath + "System", "PathToServerDynamic", null);

        string Filesource = null;
        string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        foreach (string File in FileList)
            Filesource = File;
        label.Text = Filesource;

        if (System.IO.Directory.Exists(opera3InstallationPath))
        {
            System.IO.File.Copy(Filesource, opera3InstallationPath);
            MessageBox.Show("File Copied from" + Filesource + "\n to" + opera3InstallationPath);
        }
        else
        {
            MessageBox.Show("Directory Doesn't Exist");
        }

用户将文件拖到窗口上,然后我获取应用程序的安装路径,然后将其用作源文件的目标。。当应用程序运行时,它抛出“未找到目录”错误。但是,如果目录不存在,is是否应该进入else语句?一个令人头痛的简单应用程序

您的文件源必须无效。以下是我的建议:

  • 在代码步骤中,在if(Directory.Exists(…)代码块的第一行上放置一个断点
  • 通过将文件源添加到“监视”窗口来检查文件源,检查它是否符合预期
  • 打开“立即窗口”类型File.Exists(Filesource)并检查结果(应为true)。或Directory.Exists(Path.GetDirectory(Filesource))
  • 另外,我几乎可以肯定您的代码的这一部分中存在逻辑错误。。(您在循环中一遍又一遍地分配变量,是否要附加它?这没有意义)


    它真的命中了if语句吗?是的,它通过if语句。。我知道这个目录是存在的,它是一个应用程序目录,我看不出在检查时它是如何不存在的?。。写作。可能是文件夹权限停止了应用程序对其的写入,因此引发了异常。尽管如此,如果与folderI上的安全问题有关,我会认为这是一个不同的错误。我还假设文件源引用的任何地方也存在?当用户将文件拖放到表单上时,字符串文件源会随文件位置而更新。因此,文件源实际上变成了,例如c:/user/desktop/helloworld.txt
        string Filesource = null;
        string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        foreach (string File in FileList)
            Filesource = File;
        label.Text = Filesource;