Is there a way to get the icon of a shortcut link?

Viewed 630

I'm trying to open an application by using it's shortcut file in C#. right now I am able to get it's path but I Wanted to know if I can get it's icon too or not? (I mean the icon of the application which is located in it's main folder not the icon of it's shortcut) thank you.

Right Now My Code is like this:

 public static void DoSomeHeavyLifting()
    {
        string filepath = @"C:\Users\Hello\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Adobe Photoshop 2020.lnk";

        string pathOnly = filepath;
        string filenameOnly = filepath;

        Shell shell = new Shell();
        Folder folder = shell.NameSpace(pathOnly);
        FolderItem folderItem = folder.ParseName(filenameOnly);
        if (folderItem != null)
        {
            Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
      
            Console.WriteLine(link.Path);
        }

    }

as you see I can Only Have link.Path But I want it's icon too

1 Answers

Does this work for you?

It's pretty straightforward to use just with the file path.

private void ExtractAssociatedIconEx()
{
    Icon ico =
        Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");
    this.Icon = ico;
}

Just make sure the file exists first as you will get ArgumentException if it doesn't

Related