【发布时间】:2011-10-15 20:55:33
【问题描述】:
即使在 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.0 的特性。我想问的问题是:这个函数可以转换成同样的东西,但只使用 3.5 框架吗?有什么想法吗?
【问题讨论】:
-
我觉得这个问题应该移到codereview.stackexchange.com