简体   繁体   中英

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 once 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 { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM