Even the programmatic pinning of icons in Win7 seems it's not permitted (like it says here: http://msdn.microsoft.com/en-us/library/dd378460(v=VS.85).aspx), there are some methods for doing this by some VB scripts. Someone found a way of doing this in C# like this:
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 &&开发者_Go百科amp; verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar")))
{
verb.DoIt();
}
}
shellApplication = null;
}
As can be seen, the code makes use of .NET Framework 4.0 features. The question I want to ask is: can this function be transformed so it would make the same thing, but using just 3.5 Framework? Any ideas? Thank you!
I removed the use of dynamic
and replace it with reflection calls. It's ugly and I didn't test it beyond making sure it compiles under .NET 3.5, but give this a shot. You will need to add using System.Reflection;
to your class, if you don't have it there already.
private static void PinUnpinTaskBar(string filePath, bool pin)
{
if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
// create the shell application object
var shellType = Type.GetTypeFromProgID("Shell.Application");
var shellApplication = Activator.CreateInstance(shellType);
string path = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
var directory = shellType.InvokeMember("Namespace", BindingFlags.InvokeMethod, null, shellApplication, new object[] { path });
var link = directory.GetType().InvokeMember("ParseName", BindingFlags.InvokeMethod, null, directory, new object[] {fileName});
var verbs = link.GetType().InvokeMember("Verbs", BindingFlags.InvokeMethod, null, link, new object[] { });
int verbsCount = (int)verbs.GetType().InvokeMember("Count", BindingFlags.InvokeMethod, null, verbs, new object[] { });
for (int i = 0; i < verbsCount; i++)
{
var verb = verbs.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, verbs, new object[] { i });
var namePropertyValue = (string)verb.GetType().GetProperty("Name").GetValue(verb, null);
var verbName = namePropertyValue.Replace(@"&", string.Empty).ToLower();
if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar")))
{
verbs.GetType().InvokeMember("DoIt", BindingFlags.InvokeMethod, null, verbs, new object[] { });
}
}
shellApplication = null;
}
精彩评论