使用C#将* .lnk文件固定到Windows 7任务栏

即使是Windows 7中图标的程序化固定似乎也是不允许的(就像它在这里说的那样: http : //msdn.microsoft.com/en-us/library/dd378460(v = VS.85 ) .aspx ),有一些使用一些VB脚本执行此操作的方法。 有人在C#中找到了这样做的方法:

private static void PinUnpinTaskBar(string filePath, bool pin) { if (!File.Exists(filePath)) throw new FileNotFoundException(filePath); // create the shell application object dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); dynamic directory = shellApplication.NameSpace(path); dynamic link = directory.ParseName(fileName); dynamic verbs = link.Verbs(); for (int i = 0; i < verbs.Count(); i++) { dynamic verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar"))) { verb.DoIt(); } } shellApplication = null; } 

可以看出,代码使用了.NET Framework 4.0function。 我想问的问题是:这个函数可以转换,所以它可以做同样的事情,但只使用3.5框架? 有任何想法吗?

简单…

  private static void PinUnpinTaskBar(string filePath, bool pin) { if (!File.Exists(filePath)) throw new FileNotFoundException(filePath); // create the shell application object Shell shellApplication = new ShellClass(); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); Folder directory = shellApplication.NameSpace(path); FolderItem link = directory.ParseName(fileName); FolderItemVerbs verbs = link.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar"))) { verb.DoIt(); } } shellApplication = null; } 

请务必向“Microsoft Shell控件和自动化”添加COM引用。

如果你想保留使用Activator.CreateInstance的现有方法,那么你不必拥有额外的COM互操作DLL,那么你将不得不使用reflection。 但这会使代码变得更加丑陋。

无论Windows用户使用什么本地化:

  int MAX_PATH = 255; var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH); IntPtr hShell32 = LoadLibrary("Shell32.dll"); LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH); string localizedVerb = szPinToStartLocalized.ToString(); // create the shell application object dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); dynamic directory = shellApplication.NameSpace(path); dynamic link = directory.ParseName(fileName); dynamic verbs = link.Verbs(); for (int i = 0; i < verbs.Count(); i++) { dynamic verb = verbs.Item(i); if ((pin && verb.Name.Equals(localizedVerb)) || (!pin && verb.Name.Equals(localizedVerb))) { verb.DoIt(); break; } } 

在Windows 10中,上述方法不起作用。 “Pin to Taskbar”动词不会出现在程序列表中,只会出现在资源管理器中。 为了在Windows 10中工作,您有两种选择。 将程序重命名为explorer.exe,或者您必须欺骗对象以认为您的程序名为explorer.exe。 您必须找到PEB并更改图像路径字段。 我在这里写了一篇关于如何做的post。