drag and drop options

醉酒当歌 提交于 2019-12-23 06:00:28

问题


I'm doing a simple tool. If I drag n drop files ,folders into my form it will automatically open the corresponding file and folder. Now I want to do it for .lnk files(shortcuts) if I drag a .lnk file, it must open the target file.


回答1:


Okay this is a simple mock up but you should get the idea...

First add the COM 'Windows Script Host Object Model' reference to your project.

Next include the line...

using IWshRuntimeLibrary;

For this example I just used a list box control but use what ever you want... If you handle the DragEnter event, you can get the file name passed as an argument. You can then create a WshShell object to get the links target path.

private void listBox1_DragEnter(object sender, DragEventArgs e)
{
    String[] fileName = (String[])e.Data.GetData("FileName");

    WshShell shell = new WshShell();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(fileName[0]);

    String targetPath = link.TargetPath;

    listBox1.Items.Add(targetPath);
}

The code doesn't handle non shortcuts etc but it should give you a starter... :)



来源:https://stackoverflow.com/questions/938053/drag-and-drop-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!