我再次修改我的问题并以可测试的方式重新共享完整的代码:
我正在使用此类在 C# 上创建桌面快捷方式:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace WinForms;
class Program
{
// Define the IShellLinkW interface
[ComImport]
[Guid("000214F9-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellLinkW
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, uint fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotKey(out short wHotKey);
void SetHotKey(short wHotKey);
void GetShowCmd(out int iShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int iIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, uint dwReserved);
void Resolve(IntPtr hwnd, uint fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
// Define the CLSID of ShellLink
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
class ShellLink
{
}
// Define the hotkey modifiers
const int MOD_ALT = 0x0001;
const int MOD_CONTROL = 0x0002;
const int MOD_SHIFT = 0x0004;
static void Main(string[] args)
{
// Create an instance of ShellLink
IShellLinkW link = (IShellLinkW)new ShellLink();
// Set the path of the target file
link.SetPath(@"C:\Program Files\MyApp\MyApp.exe");
// Set the working directory of the target file
link.SetWorkingDirectory(@"C:\Program Files\MyApp");
// Set the description of the shortcut
link.SetDescription("My shortcut for MyApp");
// Set the hotkey of the shortcut
short hotkey = (short)(MOD_CONTROL | MOD_ALT | (int)ConsoleKey.S);
link.SetHotKey(hotkey);
// Save the shortcut to the desktop
IPersistFile file = (IPersistFile)link;
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string shortcutPath = Path.Combine(desktopPath, "MyApp.lnk");
file.Save(shortcutPath, false);
// Release the COM objects
Marshal.FinalReleaseComObject(file);
Marshal.FinalReleaseComObject(link);
}
}
不幸的是,虽然这段代码可以创建桌面快捷方式,但它只将
S
键指定为快捷键,如下所示:
而我想分配
Ctrl + Alt + S
。在这个场景中,我如何将 Ctrl + Alt + S
分配给我的“MyApp.lnk”快捷方式?
Keys
是 enum
,它们不支持 +
运算符。
您需要先将每个转换为short
:
(short)Keys.Control + (short)Keys.Alt + (short)Keys.S
// or use bitwise operator
(short)(Keys.Control | Keys.Alt | Keys.S)