【问题标题】:Drag Drop copy file拖放复制文件
【发布时间】:2012-09-18 15:18:46
【问题描述】:

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

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");
        }

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

【问题讨论】:

  • 它真的命中了 if 语句吗?
  • 是的,它通过 if 语句.. 我知道该目录存在 dropbox.com/s/hhro4yox0wjc63o/vs.PNG
  • 这是一个应用程序目录,从检查时我看不出它怎么会不存在?...到写作。可能是文件夹权限阻止应用程序写入它,因此引发异常。虽然,如果它与文件夹上的安全问题有关,我会认为它会是一个不同的错误
  • 我还假设 Filesource 引用的地方也存在?
  • 当用户将文件拖放到表单上时,字符串文件源将更新为文件位置。所以文件源基本上变成了,例如 c:/user/desktop/helloworld.txt

标签: c# .net windows winforms forms


【解决方案1】:

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

  1. 单步执行您的代码,在 if(Directory.Exists(...)) 代码块的第一行放置一个断点。
  2. 通过将 Filesource 添加到监视窗口来检查它是否符合您的预期
  3. 打开“即时窗口”类型 File.Exists(Filesource) 并检查结果(应该为真)。或者.. Directory.Exists(Path.GetDirectory(Filesource))

另外,我几乎可以肯定您的这部分代码存在逻辑错误。(您在循环中一遍又一遍地分配一个变量,您是要附加它吗?这没有意义)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多