locked
Creating Shell Links/File Shortcuts with Framework? RRS feed

  • Question

  • What's the best way to create a link or shortcut to a file, using the 2.0 Framework?

    I know this must be an oft answered question.  But for some reason the only ways to create file system links I've been able to discover involve calling WSH, or wrapping SHELL32.dll.  Neither rely on the .Net Framework. 

    Surely there is finally support for shortcuts in the new release?

    Karen

    Tuesday, August 2, 2005 11:23 AM

Answers

  • No unfortunately there is no managed classes in the .NET Framework 1.0 or 2.0 to create links or shortcuts to a file. You will need to p-invoke shell32.dll.

    I believe the reasoning for this is that most shortcuts are created when the application is installed via Windows Installer.
    Tuesday, August 2, 2005 11:40 AM

All replies

  • No unfortunately there is no managed classes in the .NET Framework 1.0 or 2.0 to create links or shortcuts to a file. You will need to p-invoke shell32.dll.

    I believe the reasoning for this is that most shortcuts are created when the application is installed via Windows Installer.
    Tuesday, August 2, 2005 11:40 AM
  • << No unfortunately there is managed classes in the .NET Framework 1.0 or 2.0 to create links or shortcuts to a file. You will need to p-invoke shell32.dll. >>

    That is really sad.  Visual Basic programmers have had to jump through hoops to make shortcuts for more than 10 years!  Even the workarounds are needlessly complicated. 

    << I believe the reasoning for this is that most shortcuts are created when the application is installed via Windows Installer. >>

    Maybe most, but definitely not all.  Lots of programs dynamically create shortcuts. A Google search turns up hundreds of pages describing byzantine ways to get around this deficiency.  That should have given MS a clue.  :(

    Karen

    Tuesday, August 2, 2005 12:30 PM
  •  David M. Kean wrote:
     You will need to p-invoke shell32.dll.

    Can you point me to some code that demonstrates this?  What I've seen involves creating a custom .IDL file, generating a typelib from that, adding a reference to the new typelib, then creating IShellLink and IPersistFile objects to make, edit, and view the shortcuts.

    I did that already with VB6.  I'm hoping there's a simply way nowadays. 

    Karen
    Tuesday, August 2, 2005 12:33 PM
  • Karen, I tried using p-invoke with Shell32.dll (pretty messy, really), but came up with the following method that seems a lot easier (C# coded):

    1.  Add a reference to the Windows Scripting Host Object Model (1.0 currently) at C:\WINDOWS\System32\wshom.ocx using the Add Reference dialog Browse tab.

    2.  Add a "" line to the top of the source code where you need to creat the shortcut.

    3.  The following lines of code create a shortcut:

    //You can use any special folder from the enumeration or any valid Path string.
    string
    s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    IWshShell wsh = new WshShellClass();
    IWshShortcut shtct = (IWshShortcut)wsh.CreateShortcut(s + "\\Your Name.lnk");
    shtct.WindowStyle = 1; //for default window, 3 for maximize, 7 for minimize
    shtct.TargetPath =
    Application.ExecutablePath; //for me, or any valid Path string
    shtct.IconLocation =
    "notepad.exe, 0"; //of any icon you find, including your own.
    shtct.Save();

    You can also assign values for the Arguments, Description, FullName, Hotkey, RelativePath, and WorkingDirectory string properties as per the WshShorcut Object in the MSDN documentation.

    Now wasn't that easier than using Interop Services to CoCreateInstance() the IShellLink and IPersist Interfaces through use of the class ID and interface ID's and Interop Marshalling?

    Hope this helps, Gordon
    Sunday, November 27, 2005 7:31 AM
  • Whoops, forgot to add the using in step2...  Sorry.

    Karen, I tried using p-invoke with Shell32.dll (pretty messy, really), but came up with the following method that seems a lot easier (C# coded):

    1.  Add a reference to the Windows Scripting Host Object Model (1.0 currently) at C:\WINDOWS\System32\wshom.ocx using the Add Reference dialog Browse tab.

    2.  Add a "using IWshRuntimeLibrary;" line to the top of the source code where you need to creat the shortcut.

    3.  The following lines of code create a shortcut:

    //You can use any special folder from the enumeration or any valid Path string.
    string
    s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    IWshShell wsh = new WshShellClass();
    IWshShortcut shtct = (IWshShortcut)wsh.CreateShortcut(s + "\\Your Name.lnk");
    shtct.WindowStyle = 1; //for default window, 3 for maximize, 7 for minimize
    shtct.TargetPath =
    Application.ExecutablePath; //for me, or any valid Path string
    shtct.IconLocation =
    "notepad.exe, 0"; //of any icon you find, including your own.
    shtct.Save();

    You can also assign values for the Arguments, Description, FullName, Hotkey, RelativePath, and WorkingDirectory string properties as per the WshShorcut Object in the MSDN documentation.

    Now wasn't that easier than using Interop Services to CoCreateInstance() the IShellLink and IPersist Interfaces through use of the class ID and interface ID's and Interop Marshalling?

    Hope this helps, Gordon
    Sunday, November 27, 2005 7:38 AM
  • It didn't word with a Unicode filename...other way else ??

    Ex: Mãi yêu mùa hạ.mp3 <== cannot create a shortcut

    Tuesday, September 13, 2011 6:11 AM