How do you create an application shortcut (.lnk file) in C# with command line arguments

17,695

You'll need to add a COM reference to Windows Scripting Host. AFAIK, there is no native .net way to do this.

This sample will create a shortcut on the desktop. It will launch the FTP app, using the XML file set as a command line argument. Just change the values to what you need.

        WshShellClass wsh = new WshShellClass();
        IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
        shortcut.Arguments = "c:\\app\\settings1.xml";
        shortcut.TargetPath = "c:\\app\\myftp.exe";
        // not sure about what this is for
        shortcut.WindowStyle = 1; 
        shortcut.Description = "my shortcut description";
        shortcut.WorkingDirectory = "c:\\app";
        shortcut.IconLocation = "specify icon location";
        shortcut.Save();

With this code, the target type is properly populated as application and will launch the app with settings1.xml as a command line argument. From your question, I think this is what you are trying to do. However, I don't think you can create a shortcut to, let's say just an xml file, and have the target type set as application.

For .Net 4.0 and above, replace the first line with the following (Thanks to Millie Smith!):

        WshShell wsh = new WshShell();
Share:
17,695
Eric Anastas
Author by

Eric Anastas

Updated on June 05, 2022

Comments

  • Eric Anastas
    Eric Anastas almost 2 years

    I've written a simple FTP based file synchronization program. The settings of which can be stored in an XML file. I want users to be able to quickly open the program using various configurations. So I set up the program to be able to read the path to a config file through command line argument. This way shortcuts can be created on the desktop based on different config files. Now I'm trying to add a feature that will automatically create the shortcut for a specific config file.

    I've tried using the example from this post using ShellLink.cs. And I've also tried using IWshRuntimeLibrary as described here. I'm able to create the shortcut, but the "Target Type" of the shortcut in the properties window ends up being blank in the new shortcut. If I double click the shortcut I get an error window from windows about it having problems finding FooBar.xml at the path provided. So it seems like it doesn't realize that it should be starting an application not opening a file.

    Now, if I open the properties of the new shortcut and change something in the target field, and then change it back. For example delete the x from xml and then just add it back. After click OK the icon of the shortcut immediately switches to the icon of my app, and works correctly when double clicked.

    So it appears that when I edit the shortcut manually it forces windows to check if the shortcut target type should be Application and switches it.

    So how can I, through C#, create a new shortcut with a target type of Application when the target path does not end with .exe?

  • Doug
    Doug about 9 years
    Add Reference > COM > Windows Script Host Object Model
  • ThEpRoGrAmMiNgNoOb
    ThEpRoGrAmMiNgNoOb about 9 years
    Hello :) I tried the code but it returns an error in this part of the code WshShellClass wsh = new WshShellClass(); It says: Error 1 Interop type 'IWshRuntimeLibrary.WshShellClass' cannot be embedded. Use the applicable interface instead.
  • Millie Smith
    Millie Smith about 8 years
    @MichayCarpa stackoverflow.com/a/4553402/2850543 got the code to at least compile for me. The first line should be WshShell wsh = new WshShell(); for .Net 4.0 and later.
  • not_a_generic_user
    not_a_generic_user almost 2 years
    @Doug, how do you do this in VS Studio 2020+? There's no "Add reference" function anymore.