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

后端 未结 1 1784
南方客
南方客 2020-12-06 03:36

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

相关标签:
1条回答
  • 2020-12-06 04:15

    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();
    
    0 讨论(0)
提交回复
热议问题