Exemple #1
0
        /// <summary>
        /// Gets a <see cref="PropertyStore"/> using the specified <see cref="PropertyStore.GetFlags"/>
        /// </summary>
        /// <param name="flags">The flags to specify the PropertyStore retrieval method</param>
        /// <returns>The PropertyStore associated with this item</returns>
        public PropertyStore GetPropertyStore(PropertyStore.GetFlags flags)
        {
            IntPtr pUnk;

            _pItem.GetPropertyStore((GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
            PropertyStore store = new PropertyStore(pUnk);

            Marshal.Release(pUnk);
            return(store);
        }
Exemple #2
0
        /// <summary>
        /// Loads a <see cref="PropertyStore"/> for the specified file or extension. Note that if creating a PropertyStore based on extension only, this store will be read only. The <see cref="GetFlags.BestEffort"/>
        /// should be used for retrieving that store, as the file does not exist to enable the reading of properties that would be stored in it, and any other flags are likely to produce an exception.
        /// </summary>
        /// <param name="path">The path or extension of the file the store is required for</param>
        /// <param name="flags">The <see cref="GetFlags"/> indicating the type of store to load</param>
        public PropertyStore(string path, GetFlags flags)
        {
            //if (File.Exists(path)) {
            IntPtr pidl = ILCreateFromPath(Path.GetFullPath(path));

            try {
                IntPtr  shUnk = IntPtr.Zero;
                HRESULT hr    = SHCreateItemFromIDList(pidl, IID.IShellItem2, out shUnk);
                _pSource = (IShellItem2)Marshal.GetUniqueObjectForIUnknown(shUnk);
                try {
                    IntPtr pUnk = IntPtr.Zero;
                    _pSource.GetPropertyStore((GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
                    try {
                        _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
                    }
                    finally { Marshal.Release(pUnk); }
                }
                finally {
                    if (shUnk != IntPtr.Zero)
                    {
                        Marshal.Release(shUnk);
                    }
                }
            }
            finally {
                if (pidl != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pidl);
                }
            }

            /*}
             * else {
             *      string file;
             *      path = path.Substring(path.LastIndexOf('\\') + 1);
             *      if (path[0] == '.' || !path.Contains('.'))
             *              file = Path.ChangeExtension("_Fake", path);
             *      else
             *              file = Path.GetFileName(path);
             *      WIN32_FIND_DATA fd = new WIN32_FIND_DATA() { nFileSizeLow = 42 };
             *      FakeFile f = new FakeFile(ref fd, file);
             *      _pSource = (IShellItem2)f.GetShellItem();
             *      IntPtr pUnk = IntPtr.Zero;
             *      try {
             *              _pSource.GetPropertyStore((GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
             *              _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
             *      }
             *      finally {
             *              if (pUnk != IntPtr.Zero)
             *                      Marshal.Release(pUnk);
             *      }
             * }*/
        }
        public static void HashLnk(string lnkPath)
        {
            SHCreateItemFromParsingName(lnkPath, null, typeof(IShellItem2).GUID, out IShellItem item);
            IShellItem2 item2 = (IShellItem2)item;

            PSGetPropertyKeyFromName("System.Link.TargetParsingPath", out PropertyKey pk);
            //PKEY_Link_TargetParsingPath
            //pk.GUID = new Guid("{B9B4B3FC-2B51-4A42-B5D8-324146AFCF25}");
            //pk.PID = 2;
            string targetPath;

            try { targetPath = item2.GetString(pk); }
            catch { targetPath = null; }

            PSGetPropertyKeyFromName("System.Link.Arguments", out pk);
            //PKEY_Link_Arguments
            //pk.GUID = new Guid("{436F2667-14E2-4FEB-B30A-146C53B5B674}");
            //pk.PID = 100;
            string arguments;

            try { arguments = item2.GetString(pk); }
            catch { arguments = null; }

            string blob = GetGeneralizePath(targetPath) + arguments;

            blob += "do not prehash links.  this should only be done by the user.";//特殊但必须存在的字符串
            blob  = blob.ToLower();
            byte[] inBytes   = Encoding.Unicode.GetBytes(blob);
            int    byteCount = inBytes.Length;

            byte[] outBytes = new byte[byteCount];
            HashData(inBytes, byteCount, outBytes, byteCount);
            uint hash = BitConverter.ToUInt32(outBytes, 0);

            Guid           guid  = typeof(IPropertyStore).GUID;
            IPropertyStore store = item2.GetPropertyStore(GPS.READWRITE, ref guid);

            PSGetPropertyKeyFromName("System.Winx.Hash", out pk);
            //PKEY_WINX_HASH
            //pk.GUID = new Guid("{FB8D2D7B-90D1-4E34-BF60-6EAC09922BBF}");
            //pk.PID = 2;
            PropVariant pv = new PropVariant {
                VarType = VarEnum.VT_UI4, ulVal = hash
            };

            store.SetValue(ref pk, ref pv);
            store.Commit();

            Marshal.ReleaseComObject(store);
            Marshal.ReleaseComObject(item);
            PropVariantClear(pv);
        }
Exemple #4
0
 public static T GetPropertyStore <T>(this IShellItem2 psi, GETPROPERTYSTOREFLAGS flags) where T : class
 {
     return((T)psi.GetPropertyStore(flags, typeof(T).GUID));
 }