Creating a file shortcut (.lnk)

后端 未结 1 467
傲寒
傲寒 2020-12-05 10:45

I have been looking for a simple way to create a shortcut to a file in C#, but I\'ve only found external dlls that do that. It\'s actually quite surprising, there\'s no buil

相关标签:
1条回答
  • 2020-12-05 11:09

    One way to do this is pointed out by Joepro in their answer here:

    You'll need to add a COM reference to Windows Scripting Host. As far as i know, there is no native .net way to do this.

    WshShellClass wsh = new WshShellClass();
    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
        Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
    shortcut.Arguments = "";
    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();
    

    For .Net 4.0 and above, replace the first line with the following:

     WshShell wsh = new WshShell();
    

    EDIT: This link can help too

    0 讨论(0)
提交回复
热议问题