How can I tell a shortcut from a file in a C# drag and drop operation?
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 o开发者_如何学编程nce I have the name?
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?
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 { }
}
精彩评论