How do i get the path name from a file shortcut ? Getting exception [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:06:53

To get the target of a shortcut (.lnk file extension) you'll need first to have the following COM object: Windows Script Host Object Model

Then, you may use WshShell (or WshShellClass) and IWshShortcut interfaces to get the target of a shortcut

Example

            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
            } 

Thanks,
I hope you find this helpful :)


You may try the following steps to add Windows Script Host Object Model to your project

  • Under Solution Explorer, Right-click your project name and select Add Reference
  • Select the tab COM from the pop-up Window
  • Under Component Name, select Windows Script Host Object Model
  • Click on OK
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!