c# - 如何从文件快捷方式获取路径名?获取异常

标签 c# windows-shell

<分区>

Possible Duplicate:
Get target of shortcut folder

例如,在 C:\TEMP\ 中,我有一个名为 test.dll 的快捷方式,该快捷方式将指向文件名 test.dll

我只想从快捷方式获取文件本身的路径名。 所以,我在另一个递归函数中调用这个函数,每次从我的硬盘中的另一个目录中放入这个函数。

例如,第一个目录是 C:\TEMP 然后在 C:\TEMP 中有快捷方式文件,我只想获取该文件的路径.在 C:\TEMP 中进行测试,我现在有 3 个文件:

hpwins23.dat
hpwmdl23.dat
hpwmdl23.dat - Shortcut (C:\TEMP\hpwmdl23.dat)

所以,我想得到的是快捷方式的路径名,在本例中是 C:\TEMP

我尝试使用这个函数:

public string GetShortcutTargetFile(string shortcutFilename)
        {
            string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
            Shell shell = new Shell();
            Folder folder = shell.NameSpace(pathOnly);
            if (folder == null)
            {
            }
            else
            {
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    return link.Path;
                }
            }
            return string.Empty;
        }

但是当我使用该函数并使用它的快捷方式时,我在该行收到异常错误:

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink //The exception is: NotImplementedException: The method or operation is not implemented

我应该怎么做才能解决它?

这是完整的异常错误信息:

System.NotImplementedException was caught
Message=The method or operation is not implemented.
Source=GatherLinks
StackTrace:
at Shell32.FolderItem.get_GetLink()
at GatherLinks.Form1.GetShortcutTargetFile(String shortcutFilename) in
D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 904
at GatherLinks.Form1.offlinecrawling

最佳答案

要获取快捷方式的目标(.lnk 文件扩展名),您首先需要拥有以下 COM 对象:Windows Script Host Object Model

然后,您可以使用WshShell(或WshShellClass)和IWshShortcut接口(interface)来获取快捷方式的目标

示例

            string linkPathName = @"D:\Picrofo Autobot.lnk"; // Change this to the shortcut path

            if (System.IO.File.Exists(linkPathName))
            {
             // WshShellClass shell = new WshShellClass();
                WshShell shell = new WshShell(); //Create a new WshShell Interface
                IWshShortcut link = (IWshShortcut)shell.CreateShortcut(linkPathName); //Link the interface to our shortcut

                MessageBox.Show(link.TargetPath); //Show the target in a MessageBox using IWshShortcut
            } 

谢谢,
希望这对您有所帮助:)


您可以尝试以下步骤将Windows Script Host Object Model添加到您的项目

  • Solution Explorer 下,右键点击您的项目名称并选择Add Reference
  • 从弹出窗口中选择标签 COM
  • 组件名称下,选择Windows Script Host Object Model
  • 点击确定

关于c# - 如何从文件快捷方式获取路径名?获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079569/

相关文章:

C# Visual Studio - 找不到 Microsoft.VisualBasic.PowerPacks.Vs

Windows 10 相当于 LaunchAdvancedAssociationUI

Windows 批处理 : run a process in background and wait for it

c# - 在 Controller 体内访问 Web API 反序列化/序列化方法

c# - 使用 StreamSocket 升级到 SSL

c# - HTML Tidy - 添加开始标签,而不是删除结束标签?

c++ - 如何 : Display progress dialog using IFileOperation

windows api - 检测何时打开某种类型的文件

控制面板 PIDLs 奇怪的行为

C# 播放 MPEG 音频文件