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);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the string value from the specified <see cref="PropertyKey"/>
        /// </summary>
        /// <param name="key">The property being requested</param>
        /// <returns>The string value of the specified property</returns>
        public string GetString(PropertyKey key)
        {
            string sVal;

            _pItem.GetString(key.MarshalledPointer, out sVal);
            return(sVal);
        }
Beispiel #3
0
		internal static string GetItemType(IShellItem2 shellItem) {
			if (shellItem != null) {
				string itemType = null;
				HResult hr = shellItem.GetString(ref ItemTypePropertyKey, out itemType);
				if (hr == HResult.Ok) { return itemType; }
			}

			return null;
		}
Beispiel #4
0
        internal static string GetItemType(IShellItem2 shellItem)
        {
            if (shellItem != null)
            {
                string  itemType = null;
                HResult hr       = shellItem.GetString(ref ItemTypePropertyKey, out itemType);
                if (hr == HResult.Ok)
                {
                    return(itemType);
                }
            }

            return(null);
        }
Beispiel #5
0
        internal static string GetItemType(IShellItem2 shellItem)
        {
            if (shellItem != null)
            {
                string itemType = null;

                HRESULT hr = shellItem.GetString(ref ItemTypePropertyKey, out itemType);

                if (hr == HRESULT.S_OK)
                    return itemType;
            }

            return null;
        }
        private static string GetPrinterFriendlyNameIfPrinter(IShellItem2 shellItem)
        {
            // Devices_PrimaryCategory returns "Printers" for printers and faxes on Windows 10 but "Printers and faxes" on Windows 7.
            using (var categoryIds = new PropVariantSafeHandle())
            {
                shellItem.GetProperty(PKEY.Devices_CategoryIds, categoryIds).ThrowIfError();
                if (!categoryIds.ToStringVector().Any(id => id.StartsWith("PrintFax", StringComparison.OrdinalIgnoreCase)))
                {
                    return(null);
                }
            }

            // The canonical or "friendly name" needed to match the devmode
            // https://blogs.msdn.microsoft.com/asklar/2009/10/21/getting-the-printer-friendly-name-from-the-device-center-shell-folder/
            // PKEY_Devices_InterfacePaths doesn't seem to ever be found, but PKEY_Devices_FriendlyName works so...
            shellItem.GetString(PKEY.Devices_FriendlyName, out var friendlyName).ThrowIfError();
            return(friendlyName.ReadAndFree());
        }
        /// <summary>
        ///     Get the item type from the specified <see cref="IShellItem2" />.
        /// </summary>
        /// <param name="shellItem"><see cref="IShellItem2" />.</param>
        /// <returns>Item type string.</returns>
        private static string GetItemType(IShellItem2 shellItem)
        {
            Contract.Requires(shellItem != null);
            Contract.Ensures(Contract.Result <string>() != null);

            var    itemTypeKey = new PROPERTYKEY(new Guid("28636AA6-953D-11D2-B5D6-00C04FD918D0"), 11);
            string result;
            var    hr = shellItem.GetString(ref itemTypeKey, out result);

            if (HRESULT.Failed(hr))
            {
                try
                {
                    result = Path.GetExtension(GetParsingName(shellItem));
                }
                catch (Exception)
                {
                    result = String.Empty;
                }
            }

            return(result);
        }
 internal static string GetItemType(IShellItem2 shellItem) => shellItem != null && shellItem.GetString(ref ItemTypePropertyKey, out string itemType) == HResult.Ok ? itemType : null;