Example #1
0
        /// <summary>
        /// Creates a list of stack keys, as specified. If this method is not called,
        /// by default the folder will not be stacked.
        /// </summary>
        /// <param name="canonicalNames">Array of canonical names for properties on which the folder is stacked.</param>
        /// <exception cref="System.ArgumentException">If one of the given canonical names is invalid.</exception>
        public void SetStacks(params string[] canonicalNames)
        {
            if (canonicalNames == null)
            {
                throw new ArgumentNullException("canonicalNames");
            }
            List <PropertyKey> propertyKeyList = new List <PropertyKey>();

            foreach (string prop in canonicalNames)
            {
                // Get the PropertyKey using the canonicalName passed in
                PropertyKey propKey;
                int         result = PropertySystemNativeMethods.PSGetPropertyKeyFromName(prop, out propKey);

                if (!CoreErrorHelper.Succeeded(result))
                {
                    throw new ArgumentException(LocalizedMessages.ShellInvalidCanonicalName, "canonicalNames", Marshal.GetExceptionForHR(result));
                }

                propertyKeyList.Add(propKey);
            }

            if (propertyKeyList.Count > 0)
            {
                SetStacks(propertyKeyList.ToArray());
            }
        }
Example #2
0
        /// <summary>
        /// Creates a list of stack keys, as specified. If this method is not called,
        /// by default the folder will not be stacked.
        /// </summary>
        /// <param name="canonicalNames">Array of canonical names for properties on which the folder is stacked.</param>
        /// <exception cref="System.ArgumentException">If one of the given canonical names is invalid.</exception>
        public void SetStacks(params string[] canonicalNames)
        {
            if (canonicalNames == null)
            {
                throw new ArgumentNullException("canonicalNames");
            }
            List <PROPERTYKEY> propertyKeyList = new List <PROPERTYKEY>();

            foreach (string prop in canonicalNames)
            {
                // Get the PropertyKey using the canonicalName passed in
                PROPERTYKEY propKey;
                int         result = PropertySystemNativeMethods.PSGetPropertyKeyFromName(prop, out propKey);
                if (result == 0)
                {
                    throw new ArgumentException("", "canonicalNames", Marshal.GetExceptionForHR(result));
                }
                propertyKeyList.Add(propKey);
            }

            if (propertyKeyList.Count > 0)
            {
                SetStacks(propertyKeyList.ToArray());
            }
        }
        public static ShellPropertyKey FromCanonicalName(string canonicalName)
        {
            PROPERTYKEY propertyKey;
            var         hr = PropertySystemNativeMethods.PSGetPropertyKeyFromName(canonicalName, out propertyKey);

            if (HRESULT.Failed(hr))
            {
                throw new ArgumentException(
                          ErrorMessages.ShellInvalidCanonicalName,
                          ShellException.FromHRESULT(hr));
            }

            return(new ShellPropertyKey(propertyKey, canonicalName));
        }
        internal IShellProperty CreateTypedProperty(string canonicalName)
        {
            // Otherwise, call the native PropertyStore method
            PropertyKey propKey;

            int result = PropertySystemNativeMethods.PSGetPropertyKeyFromName(canonicalName, out propKey);

            if (!CoreErrorHelper.Succeeded(result))
            {
                throw new ArgumentException(
                          "The given CanonicalName is not valid.",
                          Marshal.GetExceptionForHR(result));
            }
            return(CreateTypedProperty(propKey));
        }
Example #5
0
        static void Explorer()
        {
            string explorerFile = "";

            try
            {
                Guid IPropertyStoreGuid = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99");

                PropertySystemNativeMethods.PSGetPropertyKeyFromName("System.Comment", out PropertyKey key);

                DirectoryInfo di    = new DirectoryInfo(targetDirectory);
                int           count = 0;
                bool          done  = false;
                while (!done)
                {
                    foreach (FileInfo file in di.GetFiles())
                    {
                        explorerFile = file.Name;
                        count++;
                        HResult hr = (HResult)SHGetPropertyStoreFromParsingName(file.FullName, IntPtr.Zero,
                                                                                GETPROPERTYSTOREFLAGS.GPS_READWRITE, ref IPropertyStoreGuid, out IPropertyStore ps);

                        if (hr == HResult.Ok)
                        {
                            try
                            {
                                PropVariant val = new PropVariant();
                                hr = ps.GetValue(key, val);
                                if (hr == HResult.Ok)
                                {
                                    var sValue = val.ToString();

                                    if (count % 30 == 0)
                                    {
                                        PropVariant value = new PropVariant("test comment");
                                        hr = ps.SetValue(key, value);
                                        if (hr == HResult.Ok)
                                        {
                                            hr = ps.Commit();
                                            if (hr == HResult.Ok)
                                            {
                                                LogLine(3, $"Explorer Read '{sValue}' and updated");
                                            }
                                            else if ((int)hr > 0)
                                            {
                                                LogLine(2, $"Commit for {explorerFile} returned {hr:X}");
                                            }
                                            else
                                            {
                                                throw new Exception($"Commit failed with 0x{hr:X}");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception($"SetValue failed with 0x{hr:X}");
                                        }
                                    }
                                    else
                                    {
                                        LogLine(3, $"Explorer Read '{sValue}'");
                                    }
                                }
                                else if ((uint)hr == 0x80030021)
                                {
                                    LogLine(2, $"GetValue for {explorerFile} failed with STG_E_LOCKVIOLATION");
                                    IncrementExplorerFail();
                                }
                                else
                                {
                                    throw new Exception($"GetValue failed with 0x{hr:X}");
                                }

                                done = IncrementExplorer();
                                if (done)
                                {
                                    break;
                                }
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(ps);  // optional GC preempt
                            }
                        }
                        else if ((uint)hr == 0x80030021)
                        {
                            LogLine(2, $"Explorer Open failed on '{explorerFile}' with lock violation 0x{hr:X}");
                            IncrementExplorerFail();
                        }
                        else
                        {
                            throw new Exception($"Open failed with 0x{hr:X}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLine(1, $"Explorer terminated for '{explorerFile}': {ex}");
                IncrementExplorerTerminated();
            }
        }