其实很简单,使用 API 函数 ShellExecute,就可以解决这个问题。
首先添加引用 using System.Runtime.InteropServices;
代码如下:


1 using System; 2 using System.Runtime.InteropServices; 3 4 namespace WINLOCK 5 { 6 public class APIHelper 7 { 8 [DllImport("shell32.dll")] 9 public static extern IntPtr ShellExecute( 10 IntPtr hwnd, 11 string lpOperation, 12 string lpFile, 13 string lpParameters, 14 string lpDirectory, 15 ShowCommands nShowCmd); 16 } 17 18 public enum ShowCommands : int 19 { 20 SW_HIDE = 0, 21 SW_SHOWNORMAL = 1, 22 SW_NORMAL = 1, 23 SW_SHOWMINIMIZED = 2, 24 SW_SHOWMAXIMIZED = 3, 25 SW_MAXIMIZE = 3, 26 SW_SHOWNOACTIVATE = 4, 27 SW_SHOW = 5, 28 SW_MINIMIZE = 6, 29 SW_SHOWMINNOACTIVE = 7, 30 SW_SHOWNA = 8, 31 SW_RESTORE = 9, 32 SW_SHOWDEFAULT = 10, 33 SW_FORCEMINIMIZE = 11, 34 SW_MAX = 11 35 } 36 }
调用方法:
ShellExecute(IntPtr.Zero, "TaskbarPin", FileName, null, null, ShowCommands.SW_SHOW);
其中,参数 "TaskbarPin" 为锁定,如果需要解锁,使用 "TaskbarunPin";
FileName 为 *.lnk 的绝对路径(注:一定要是快捷方式);
返回值为 IntPtr 句柄类型,大于32,说明执行成功。
当路径不存在或者该快捷方式已经锁定到任务栏时,会执行失败。