参考:https://stackoverflow.com/questions/4897655/create-a-shortcut-on-desktop

    1.不带COM库引用方法(方法内部引用,不需要在程序手动添加引用)

    1. using System.Text;
    2. using System.Linq;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Runtime.InteropServices;
    6. using System.Runtime.InteropServices.ComTypes;
    7. static void Main(string[] args)
    8. {
    9. IShellLink link = (IShellLink)new ShellLink();
    10. link.SetPath(@"D:\test\gif\GifCam.exe");//目标
    11. link.SetArguments(" -run 1");//启动参数
    12. link.SetWorkingDirectory(@"D:\test\gif");//起始位置
    13. link.SetHotkey((short)((short)Hotkey.Ctrl | (short)Hotkey.Alt | (short)'1'));//快捷键
    14. link.SetShowCmd((int)CmdShow.Maximized);//显示窗口方式
    15. link.SetDescription("Gif录制");//描述
    16. link.SetIconLocation(@"D:\test\gif\GifCam.exe", 0);//设置图标
    17. IPersistFile file = (IPersistFile)link;
    18. string path = @"D:\test\gif\GifCam.lnk";//快捷方式位置
    19. file.Save(path, false);
    20. }
    21. //https://stackoverflow.com/questions/4897655/create-a-shortcut-on-desktop
    22. [ComImport]
    23. [Guid("00021401-0000-0000-C000-000000000046")]
    24. public class ShellLink
    25. {
    26. }
    27. [ComImport]
    28. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    29. [Guid("000214F9-0000-0000-C000-000000000046")]
    30. public interface IShellLink
    31. {
    32. void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
    33. void GetIDList(out IntPtr ppidl);
    34. void SetIDList(IntPtr pidl);
    35. void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
    36. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
    37. void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
    38. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
    39. void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
    40. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
    41. void GetHotkey(out short pwHotkey);
    42. void SetHotkey(short wHotkey);
    43. void GetShowCmd(out int piShowCmd);
    44. void SetShowCmd(int iShowCmd);
    45. void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
    46. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
    47. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
    48. void Resolve(IntPtr hwnd, int fFlags);
    49. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
    50. }
    51. /// <summary>
    52. /// 键盘映射键
    53. /// </summary>
    54. /// <remarks>
    55. /// <para> 字母和数字参考ASCII字符表 http://ascii.911cha.com/ </para>
    56. /// <para> 参考微软文档的Hotkey https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelllinka-gethotkey </para>
    57. /// <para> Hotkey对应的值 http://cn.voidcc.com/question/p-vggohlrw-zm.html </para>
    58. /// <para> 小键盘和F1-F12 参考 <see cref="System.Windows.Forms.Keys"/> </para>
    59. /// </remarks>
    60. [Flags]
    61. public enum Hotkey
    62. {
    63. /*
    64. //无效内容,设置进去变成 NumPad4,NumPad8,NumPad6,NumPad2
    65. Left = 37,
    66. Up = 38,
    67. Right = 39,
    68. Down = 40,
    69. */
    70. /*小键盘的值*/
    71. NumPad0 = 96,
    72. NumPad1 = 97,
    73. NumPad2 = 98,
    74. NumPad3 = 99,
    75. NumPad4 = 100,
    76. NumPad5 = 101,
    77. NumPad6 = 102,
    78. NumPad7 = 103,
    79. NumPad8 = 104,
    80. NumPad9 = 105,
    81. F1 = 112,
    82. F2 = 113,
    83. F3 = 114,
    84. F4 = 115,
    85. F5 = 116,
    86. F6 = 117,
    87. F7 = 118,
    88. F8 = 119,
    89. F9 = 120,
    90. F10 = 121,
    91. F11 = 122,
    92. F12 = 123,
    93. Shift = 256,
    94. Ctrl = 512,
    95. Alt = 1024,
    96. Ext = 2048,
    97. }
    98. /// <summary>
    99. /// 显示方式
    100. /// </summary>
    101. /// <remarks>
    102. /// 参考
    103. /// https://docs.microsoft.com/zh-cn/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelllinka-getshowcmd
    104. /// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
    105. /// </remarks>
    106. public enum CmdShow
    107. {
    108. /// <summary>
    109. /// 激活并显示一个窗口。如果窗口被最小化或最大化,系统会将其恢复到原来的大小和位置。应用程序应在第一次显示窗口时指定此标志。
    110. /// </summary>
    111. Normal = 1,
    112. /// <summary>
    113. /// 设置无效,设置为最小化还是常规窗口
    114. /// 激活窗口并将其显示为最小化的窗口。
    115. /// </summary>
    116. Minimized = 2,
    117. /// <summary>
    118. /// 激活窗口并将其显示为最大化窗口。
    119. /// </summary>
    120. Maximized = 3,
    121. }

    Snipaste_2021-12-13_16-34-41.jpg

    2.带COM库
    需要添加 COM引用 Windows Script Host Object Model

    using IWshRuntimeLibrary;
    static void Main(string[] args)
    {
        IWshShortcut shortcut = (IWshShortcut)new WshShell().CreateShortcut(@"D:\test\gif\GifCam.lnk");//创建快捷方式对象
        shortcut.TargetPath = @"D:\test\gif\GifCam.exe";//指定目标路径
        shortcut.WorkingDirectory = @"D:\test\gif";//设置起始位置
        shortcut.WindowStyle = 1;//设置窗口显示方式,1=常规窗口,2=最小化(无效),3=最大化
        shortcut.Description = "Gif录制2";//设置备注
        shortcut.Hotkey = "Ctrl+Alt+Left";//设置快捷键
        shortcut.IconLocation = @"D:\test\gif\GifCam.exe,0";//设置图标
        shortcut.Arguments = " -run 3";//启动参数
        shortcut.Save();//保存快捷方式
    }