简体   繁体   中英

Pin lnk file to win 7 Taskbar using C#

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 && 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM