我创建了一个阻力,并在C#降控制,让人们文件拖放到自己的状态。下面是我遇到的问题,它工作正常时,它正在调试;然而,在管理员模式下运行我的程序时,这是行不通的。有什么原因?
下面是我的code:
私人无效panel1_DragEnter(对象发件人,DragEventArgs E)
{
如果(e.Data.GetData present(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
其他
e.Effect = DragDropEffects.None;
}
串startDir;
私人无效panel1_DragDrop(对象发件人,DragEventArgs E)
{
字符串[]文件=(字符串[])e.Data.GetData(DataFormats.FileDrop);
dropZoneLabel.Text =添加文件,请稍候......;
的foreach(在文件中字符串的文件)
{
布尔isFolder = File.GetAttributes(文件).HasFlag(FileAttributes.Directory);
如果(isFolder)
{
//扫描所有文件的文件夹
DirectoryOperations searchFolders =新DirectoryOperations();
DirectoryInfo的迪=新的DirectoryInfo(文件);
的foreach(FileInfo的dropfile在searchFolders.FullDirList(二,*))
{
listBox1.Items.Add(dropfile.Name);
}
startDir = di.FullName;
}
其他
{
//这是一个文件,以便将其添加为正常
listBox1.Items.Add(文件);
}
}
dropZoneLabel.Text =删除文件或文件夹位置;
}
解决方案
从开始的Windows Vista,因为用户界面特权隔离您不能拖动和以更低的完整性级别运行在更高层次的应用程序运行的应用程序下降。
请参阅本文的详细信息:为什么不拖动和拖放工作时,我的应用程序正在运行高架?
I created a drag and drop control within C# to allow people to drop files onto my form. Here's the problem I'm having, it works fine when it's being debugged; however when running my program in administrator mode it doesn't work. Is there any reason for this?
Here's my code:
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";
}
解决方案
Starting from Windows Vista because of User Interface Privilege Isolation you cannot drag and drop from an application running at lower integrity level to an application which runs on a higher level.
See this article for more details: Why Doesn’t Drag-and-Drop work when my Application is Running Elevated?