拖放不在C#中工作

时间:2014-02-08 20:41:12

标签: c# .net drag-and-drop admin

我在C#中创建了一个拖放控件,允许人们将文件拖放到我的表单上。这是我遇到的问题,它在被调试时工作正常;但是,当以管理员模式运行我的程序时,它不起作用。这有什么理由吗?

这是我的代码:

private void panel1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
}

string startDir;

private void panel1_DragDrop(object sender, DragEventArgs e)
{
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    dropZoneLabel.Text = "Adding files; please wait...";
    foreach (string file in files)
    {
        bool isFolder = File.GetAttributes(file).HasFlag(FileAttributes.Directory);
        if (isFolder)
        {
            //Scan the folder for all files
            DirectoryOperations searchFolders = new DirectoryOperations();
            DirectoryInfo di = new DirectoryInfo(file);
            foreach (FileInfo dropfile in searchFolders.FullDirList(di, "*"))
            {
                listBox1.Items.Add(dropfile.Name);
            }
            startDir = di.FullName;
        }
        else
        {
            //It's a file so add it as normal
            listBox1.Items.Add(file);
        }
    }
    dropZoneLabel.Text = "Drop files or folders here";
}

1 个答案:

答案 0 :(得分:5)

由于用户界面权限隔离而从Windows Vista开始,您无法将运行在较低完整性级别的应用程序拖放到运行在较高级别的应用程序。

有关详细信息,请参阅此文章:Why Doesn’t Drag-and-Drop work when my Application is Running Elevated?