예제 #1
0
        static void PinUnpinTaskBar(bool pin)
        {
            string filePath = Process.GetCurrentProcess().MainModule.FileName;

            // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
            int           actionIndex           = pin ? 5386 : 5387;
            StringBuilder szPinToStartLocalized = new StringBuilder(2048);
            IntPtr        hShell32 = NM.LoadLibrary("Shell32.dll");

            if (hShell32 == IntPtr.Zero)
            {
                throw new Exception("Failed to load Shell32.dll: Last Error was " + Marshal.GetLastWin32Error().ToString());
            }

            NM.LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, szPinToStartLocalized.Capacity);
            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;
                }
            }
        }