如何以编程方式将应用程序固定到任务栏
How to pin application into taskbar programmatically
我已经尝试过这段代码,它可以很好地从 Windows 10 中的任务栏取消固定应用程序,但它无法将应用程序固定到任务栏。
public static void PinUnpinTaskbar(bool pin)
{
string l_strFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
if (!File.Exists(l_strFilePath)) throw new FileNotFoundException(l_strFilePath);
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
//uncomment the following line to pin to start instead
//actionIndex = pin ? 51201 : 51394;
StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
IntPtr hShell32 = LoadLibrary("Shell32.dll");
LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
string localizedVerb = szPinToStartLocalized.ToString();
string path = Path.GetDirectoryName(l_strFilePath);
string fileName = Path.GetFileName(l_strFilePath);
// create the shell application object
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
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 (verb.Name.Equals(localizedVerb))
{
verb.DoIt();
return;
}
}
return;
}
在您的解决方案中看不出有什么问题,也尝试了几次,但该动词已不存在。经过一些研究,我发现了这个:
Update KB3093266 removes shell.Application object 'taskbarpin' verb
Update KB3093266 removes shell.Application object 'taskbarpin' verb
for adding taskbar pin item pins
It is most likely broken by one of the updates that KB3093266
supersedes
还有这个(Powershell 但同一个库):Pin to Taskbar fails in Windows 10
我已经尝试过这段代码,它可以很好地从 Windows 10 中的任务栏取消固定应用程序,但它无法将应用程序固定到任务栏。
public static void PinUnpinTaskbar(bool pin)
{
string l_strFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
if (!File.Exists(l_strFilePath)) throw new FileNotFoundException(l_strFilePath);
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
//uncomment the following line to pin to start instead
//actionIndex = pin ? 51201 : 51394;
StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
IntPtr hShell32 = LoadLibrary("Shell32.dll");
LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
string localizedVerb = szPinToStartLocalized.ToString();
string path = Path.GetDirectoryName(l_strFilePath);
string fileName = Path.GetFileName(l_strFilePath);
// create the shell application object
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
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 (verb.Name.Equals(localizedVerb))
{
verb.DoIt();
return;
}
}
return;
}
在您的解决方案中看不出有什么问题,也尝试了几次,但该动词已不存在。经过一些研究,我发现了这个:
Update KB3093266 removes shell.Application object 'taskbarpin' verb
Update KB3093266 removes shell.Application object 'taskbarpin' verb for adding taskbar pin item pins
It is most likely broken by one of the updates that KB3093266 supersedes
还有这个(Powershell 但同一个库):Pin to Taskbar fails in Windows 10