How can I tell a shortcut from a file in a C# drag and drop operation?

耗尽温柔 提交于 2019-12-24 19:12:19

问题


I have a C# .NET 3.5 app that I have incorporated the DragDrop event on a DataGridView.

#region File Browser - Drag and Drop Ops
private void dataGridView_fileListing_DragDrop(object sender, DragEventArgs e)
{
    string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
    foreach (string fileName in fileList)
    {
       //logic goes here
    }
}

My question is, how can I differentiate a windows shortcut from an actual file? I tried:

File.exists(fileName)

in an IF block which is useful to filter out directories that have been dragged in, however shortcuts get through. Is there anyway on to tell a shortcut in the data passed in by the event data, or by querying the file system once I have the name?


回答1:


A Windows shortcut is a file, just with a .lnk extension.

Could you elaborate more about what you hope to do or not do with it?




回答2:


If you need to go further and process the files or folders the shortcut is targeting, you might want to look at this http://www.codeproject.com/KB/dotnet/shelllink.aspx.

The project shows how to use Windows Scripting Host to manipulate shortcuts. For example, after creating a runtime callable wrapper (IWshRuntimeLibrary.dll) and adding this to your project, you can get the target of the shortcuts like this...

string targetPath;
if (System.IO.Path.GetExtension(path) == ".lnk"){
try{
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path);
targetPath = shortcut.TargetPath;
}
catch { }
}



来源:https://stackoverflow.com/questions/3207732/how-can-i-tell-a-shortcut-from-a-file-in-a-c-sharp-drag-and-drop-operation

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