简体   繁体   English

如何在C#拖放操作中分辨文件中的快捷方式?

[英]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. 我有一个C#.NET 3.5应用程序,该应用程序已在DataGridView上合并了DragDrop事件。

#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? 我的问题是,如何区分Windows快捷方式和实际文件? 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. 在IF块中,这有助于过滤出已拖入的目录,但是快捷方式可以通过。 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. Windows快捷方式是一个文件,扩展名为.lnk。

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 . 如果需要进一步处理快捷方式所针对的文件或文件夹,则可能需要查看此http://www.codeproject.com/KB/dotnet/shelllink.aspx

The project shows how to use Windows Scripting Host to manipulate shortcuts. 该项目显示了如何使用Windows Scripting Host操作快捷方式。 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... 例如,在创建运行时可调用包装器(IWshRuntimeLibrary.dll)并将其添加到项目中之后,您可以获取诸如此类的快捷方式的目标。

string targetPath; 字符串targetPath;
if (System.IO.Path.GetExtension(path) == ".lnk"){ 如果(System.IO.Path.GetExtension(path)==“ .lnk”){
try{ 尝试{
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.WshShell shell =新的IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path); IWshRuntimeLibrary.IWshShortcut快捷方式=(IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path);
targetPath = shortcut.TargetPath; targetPath = Shortcut.TargetPath;
} }
catch { } 赶上{}
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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