private void PopulateSystemProperties() { IPropertyDescriptionList propertyDescriptionList = null; IPropertyDescription propertyDescription = null; Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); try { int hr = PropertySystemNativeMethods.PSEnumeratePropertyDescriptions(PropertySystemNativeMethods.PropDescEnumFilter.PDEF_ALL, ref guid, out propertyDescriptionList); if (hr >= 0) { uint count; propertyDescriptionList.GetCount(out count); guid = new Guid(ShellIIDGuid.IPropertyDescription); Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(); for (uint i = 0; i < count; i++) { propertyDescriptionList.GetAt(i, ref guid, out propertyDescription); var spd = new ShellPropertyDescription(propertyDescription); propDescs.Add(spd); propDescDict.Add(spd.CanonicalName, spd); } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } } }
/// <summary> /// Releases the internal COM IPropertyDescriptionList reference /// </summary> public void Dispose() { if (_propList != null) { Marshal.FinalReleaseComObject(_propList); _propList = null; } GC.SuppressFinalize(this); }
/// <summary> /// Specifies which properties will be collected in the save dialog. /// </summary> /// <param name="appendDefault">True to show default properties for the currently selected /// filetype in addition to the properties specified by propertyList. False to show only properties /// specified by pList. /// <param name="propertyList">List of properties to collect. This parameter can be null.</param> /// </param> /// <remarks> /// SetCollectedPropertyKeys can be called at any time before the dialog is displayed or while it /// is visible. If different properties are to be collected depending on the chosen filetype, /// then SetCollectedProperties can be called in response to CommonFileDialog::FileTypeChanged event. /// Note: By default, no properties are collected in the save dialog. /// </remarks> public void SetCollectedPropertyKeys(bool appendDefault, params PropertyKey[] propertyList) { // Loop through all our property keys and create a semicolon-delimited property list string. // The string we pass to PSGetPropertyDescriptionListFromString must // start with "prop:", followed a list of canonical names for each // property that is to collected. #pragma warning disable CS8073 // The result of the expression is always 'true' since a value of type 'PropertyKey' is never equal to 'null' of type 'PropertyKey?' if (propertyList != null && propertyList.Length > 0 && propertyList[0] != null) #pragma warning restore CS8073 // The result of the expression is always 'true' since a value of type 'PropertyKey' is never equal to 'null' of type 'PropertyKey?' { StringBuilder sb = new StringBuilder("prop:"); foreach (PropertyKey key in propertyList) { string canonicalName = ShellPropertyDescriptionsCache.Cache.GetPropertyDescription(key).CanonicalName; if (!string.IsNullOrEmpty(canonicalName)) { sb.AppendFormat("{0};", canonicalName); } } Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); IPropertyDescriptionList propertyDescriptionList = null; try { int hr = PropertySystemNativeMethods.PSGetPropertyDescriptionListFromString( sb.ToString(), ref guid, out propertyDescriptionList); // If we get a IPropertyDescriptionList, setit on the native dialog. if (CoreErrorHelper.Succeeded(hr)) { InitializeNativeFileDialog(); IFileSaveDialog nativeDialog = GetNativeFileDialog() as IFileSaveDialog; if (nativeDialog != null) { hr = nativeDialog.SetCollectedProperties(propertyDescriptionList, appendDefault); if (!CoreErrorHelper.Succeeded(hr)) { throw new ShellException(hr); } } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } } } }
private IPropertyDescription getPropertyDescriptionByName(IPropertyDescriptionList oPropDescs, string pName) { IEnumerator itr = oPropDescs.GetEnumerator(); while (itr.MoveNext()) { IPropertyDescription pds = (IPropertyDescription)itr.Current; if (pds.Name == pName) { return(pds); } } return(null); }
/// <summary> /// Creates a <see cref="PropertyDescriptionList"/> based on properties defined for a particular file or file type. /// </summary> /// <param name="path">The path or extension of the file type you want the PropertyDescriptionList for</param> /// <param name="propListKey">This <see cref="PropertyKey"/> defines the usage of the properties defined in the PropertyDescriptionList. It will be one of the /// <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ff521713(v=vs.85).aspx">PropList</a> values</param> public PropertyDescriptionList(string path, PropertyKey propListKey) { IShellItem2 item = null; FakeFile file = null; if (File.Exists(path)) { IntPtr itemUnk; IntPtr pidl = ILCreateFromPath(Path.GetFullPath(path)); SHCreateItemFromIDList(pidl, IID.IShellItem2, out itemUnk); item = (IShellItem2)Marshal.GetUniqueObjectForIUnknown(itemUnk); Marshal.Release(itemUnk); Marshal.FreeCoTaskMem(pidl); } else { string name; path = path.Substring(path.LastIndexOf('\\') + 1); if (path[0] == '.' || !(path.Contains('.'))) { name = Path.ChangeExtension("_fakeFile", path); } else { name = Path.GetFileName(path); } WIN32_FIND_DATA fd = new WIN32_FIND_DATA() { dwFileAttributes = FileAttributes.Normal, nFileSizeLow = 42 }; file = new FakeFile(ref fd, name); item = (IShellItem2)file.GetShellItem(); } try { IntPtr ppUnk = IntPtr.Zero;; item.GetPropertyDescriptionList(propListKey.MarshalledPointer, IID.IProperyDescriptionList, out ppUnk); _propList = (IPropertyDescriptionList)Marshal.GetUniqueObjectForIUnknown(ppUnk); Marshal.Release(ppUnk); } finally { if (item != null) { Marshal.FinalReleaseComObject(item); } if (file != null) { file.Dispose(); } } }
/// <summary> /// Creates a <see cref="PropertyDescriptionList"/> containing the <see cref="PropertyDescription"/>s defined for <paramref name="strPropList"/> /// </summary> /// <param name="strPropList">A string declaring which properties to include in this PropertyDescriptionList. The format of this string must be "prop:prop1;prop2;...;propN" /// where "prop1 - propN" are the Canonical names of the required <see cref="PropertyKey"/>s</param> public PropertyDescriptionList(string strPropList) { IntPtr pUnk; HRESULT hr = PSGetPropertyDescriptionListFromString(strPropList, IID.IProperyDescriptionList, out pUnk); if (hr.Failed) { throw hr.GetException(); } try { _propList = (IPropertyDescriptionList)Marshal.GetUniqueObjectForIUnknown(pUnk); } finally { Marshal.Release(pUnk); } }
public void GetTextAssociation_QuickTip() { string value = ShellMethods.AssocQueryString(AssociationFlags.None, AssociationString.QuickTip, ".txt", null); value.Should().Be("prop:System.ItemTypeText;System.Size;System.DateModified"); IPropertyDescriptionList list = ShellMethods.GetPropertyDescriptionListFromString(value); list.GetCount().Should().Be(3); IPropertyDescription desc = list.GetAt(0, new Guid(InterfaceIds.IID_IPropertyDescription)); desc.GetCanonicalName().Should().Be("System.ItemTypeText"); desc = list.GetAt(1, new Guid(InterfaceIds.IID_IPropertyDescription)); desc.GetCanonicalName().Should().Be("System.Size"); desc = list.GetAt(2, new Guid(InterfaceIds.IID_IPropertyDescription)); desc.GetCanonicalName().Should().Be("System.DateModified"); }
/// <summary> /// Creates a <see cref="PropertyDescriptionList"/> containing <see cref="PropertyDescription"/>s for each property in <paramref name="properties"/> /// </summary> /// <param name="properties">An <see cref="IEnumerable{T}"/> of strings containing the canonical names for the required <see cref="PropertyKey"/>s</param> public PropertyDescriptionList(IEnumerable <string> properties) { string strPropList = "prop:" + string.Join(";", properties); IntPtr pUnk; HRESULT hr = PSGetPropertyDescriptionListFromString(strPropList, IID.IProperyDescriptionList, out pUnk); if (hr.Failed) { throw hr.GetException(); } try { _propList = (IPropertyDescriptionList)Marshal.GetUniqueObjectForIUnknown(pUnk); } finally { Marshal.Release(pUnk); } }
private void PopulatePropertyList(List <PropertyConfig> propertyList, PropertySystemNativeMethods.PropDescEnumFilter filter) { propertyList.Clear(); IPropertyDescriptionList propertyDescriptionList = null; IPropertyDescription propertyDescription = null; Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); try { var hr = PropertySystemNativeMethods.PSEnumeratePropertyDescriptions( filter, ref guid, out propertyDescriptionList); if (hr >= 0) { propertyDescriptionList.GetCount(out uint count); guid = new Guid(ShellIIDGuid.IPropertyDescription); for (uint i = 0; i < count; i++) { propertyDescriptionList.GetAt(i, ref guid, out propertyDescription); if (propertyDescription != null) { var shellProperty = new ShellPropertyDescription(propertyDescription); var pc = new PropertyConfig(shellProperty); shellProperty.Dispose(); // Releases propertyDescription propertyDescription = null; GetInstalledProperty(pc, true); // Add search and alias info propertyList.Add(pc); DictInstalledProperties.Add(pc.CanonicalName, pc); } } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } if (propertyDescription != null) { Marshal.ReleaseComObject(propertyDescription); } } }
public static DataTable GetPropertyDefinitions() { DataTable dt = CreateTable(); Guid riid; IPropertyDescriptionList propList = null; IPropertyDescription propDesc = null; try { riid = new Guid(IIDStrings.IPropertyDescriptionList); int hr = PSEnumeratePropertyDescriptions(0, // PDEF_ALL, ref riid, out propList); if (hr < 0) { throw (new ApplicationException(string.Format("PSEnumeratePropertyDescriptions returned: {0}", hr))); } uint numProps = propList.GetCount(); riid = new Guid(IIDStrings.IPropertyDescription); for (int i = 0; i < numProps; i++) { // Iterates through each item in the description list propDesc = propList.GetAt((uint)i, ref riid); string propName = propDesc.GetCanonicalName(); DataRow dr = dt.Rows.Add(propName); InitRow(dr, propDesc); Marshal.ReleaseComObject(propDesc); } } finally { Marshal.ReleaseComObject(propList); Marshal.ReleaseComObject(propDesc); } return(dt); }
public static IEnumerable <string> GetPropertyDefinitions() { Guid riid; IPropertyDescriptionList propList = null; IPropertyDescription propDesc = null; try { riid = new Guid(IIDStrings.IPropertyDescriptionList); int hr = PSEnumeratePropertyDescriptions(0, // PDEF_ALL, ref riid, out propList); if (hr < 0) { throw (new ApplicationException(string.Format("PSEnumeratePropertyDescriptions returned: {0}", hr))); } uint numProps = propList.GetCount(); riid = new Guid(IIDStrings.IPropertyDescription); for (int i = 0; i < numProps; i++) { // Iterates through each item in the description list propDesc = propList.GetAt((uint)i, ref riid); yield return(propDesc.GetCanonicalName()); } } finally { Marshal.ReleaseComObject(propList); Marshal.ReleaseComObject(propDesc); } }
public void PopulateSystemProperties() { IPropertyDescriptionList propertyDescriptionList = null; IPropertyDescription propertyDescription = null; Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); try { int hr = PropertySystemNativeMethods.PSEnumeratePropertyDescriptions(PropertySystemNativeMethods.PropDescEnumFilter.PDEF_ALL, ref guid, out propertyDescriptionList); if (hr >= 0) { uint count; propertyDescriptionList.GetCount(out count); guid = new Guid(ShellIIDGuid.IPropertyDescription); Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(); for (uint i = 0; i < count; i++) { propertyDescriptionList.GetAt(i, ref guid, out propertyDescription); string propName; propertyDescription.GetCanonicalName(out propName); List <string> names = null; string[] parts = propName.Split('.'); if (parts.Count() == 2) { // System.Foo if (!dict.TryGetValue(parts[0], out names)) { names = new List <string>(); dict.Add(parts[0], names); } names.Add(parts[1]); } else if (parts.Count() == 3) { // System.Bar.Baz if (!dict.TryGetValue(parts[1], out names)) { names = new List <string>(); dict.Add(parts[1], names); } names.Add(parts[2]); } // If we ever need it: // ShellPropertyDescription desc = new ShellPropertyDescription(propertyDescription); if (propertyDescription != null) { Marshal.ReleaseComObject(propertyDescription); propertyDescription = null; } } // build tree foreach (string cat in dict.Keys) { TreeItem main = new TreeItem(cat, PropType.Group); foreach (string name in dict[cat]) { main.AddChild(new TreeItem(name, PropType.Normal)); } if (cat == "System") { AllProperties.Insert(0, main); } else if (cat == "PropGroup") { foreach (TreeItem ti in main.Children) { GroupProperties.Add(ti.Name); } } else { AllProperties.Add(main); } } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } if (propertyDescription != null) { Marshal.ReleaseComObject(propertyDescription); } } }
public static extern HResult PSGetPropertyDescriptionListFromString( string pszPropList, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.Interface)] out IPropertyDescriptionList ppv);
internal static extern int PSEnumeratePropertyDescriptions( [In] PropDescEnumFilter filter, [In] ref Guid riid, out IPropertyDescriptionList ppv );
public void PopulateSystemProperties() { List <SystemProperty> systemProperties = new List <SystemProperty>(); IPropertyDescriptionList propertyDescriptionList = null; IPropertyDescription propertyDescription = null; Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); try { int hr = PropertySystemNativeMethods.PSEnumeratePropertyDescriptions(PropertySystemNativeMethods.PropDescEnumFilter.PDEF_ALL, ref guid, out propertyDescriptionList); if (hr >= 0) { uint count; propertyDescriptionList.GetCount(out count); guid = new Guid(ShellIIDGuid.IPropertyDescription); for (uint i = 0; i < count; i++) { propertyDescriptionList.GetAt(i, ref guid, out propertyDescription); string propName; propertyDescription.GetCanonicalName(out propName); IntPtr displayNamePtr; string displayName = null; propertyDescription.GetDisplayName(out displayNamePtr); if (displayNamePtr != IntPtr.Zero) { displayName = Marshal.PtrToStringAuto(displayNamePtr); } SystemProperty sp = new SystemProperty { FullName = propName, DisplayName = displayName }; systemProperties.Add(sp); // If we ever need it: // ShellPropertyDescription desc = new ShellPropertyDescription(propertyDescription); if (propertyDescription != null) { Marshal.ReleaseComObject(propertyDescription); propertyDescription = null; } } Dictionary <string, TreeItem> dict = new Dictionary <string, TreeItem>(); List <TreeItem> roots = new List <TreeItem>(); // Build tree based on property names foreach (SystemProperty sp in systemProperties) { AddTreeItem(dict, roots, sp); } // Wire trees to tree controls, tweaking the structure as we go TreeItem propGroup = null; foreach (TreeItem root in roots) { if (root.Name == "System") { AllProperties.Insert(0, root); // Move property groups from root to their own list propGroup = root.Children.Where(x => x.Name == "PropGroup").FirstOrDefault(); if (propGroup != null) { foreach (TreeItem ti in propGroup.Children) { GroupProperties.Add(ti.Name); } root.RemoveChild(propGroup); } // Make remaining children of System that are parents into roots List <TreeItem> movers = new List <TreeItem>(root.Children.Where(x => x.Children.Count() > 0)); foreach (TreeItem ti in movers) { root.RemoveChild(ti); AllProperties.Add(ti); } } else { AllProperties.Add(root); } } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } if (propertyDescription != null) { Marshal.ReleaseComObject(propertyDescription); } } }
public static extern int PSEnumeratePropertyDescriptions( int filterOn, [In] ref Guid riid, out IPropertyDescriptionList ppv);
internal PropertyDescriptionList(IntPtr pUnk) { _propList = (IPropertyDescriptionList)Marshal.GetUniqueObjectForIUnknown(pUnk); }
/// <summary> /// Specifies which properties will be collected in the save dialog. /// </summary> /// <param name="appendDefault">True to show default properties for the currently selected /// filetype in addition to the properties specified by propertyList. False to show only properties /// specified by pList. /// <param name="propertyList">List of properties to collect. This parameter can be null.</param> /// </param> /// <remarks> /// SetCollectedPropertyKeys can be called at any time before the dialog is displayed or while it /// is visible. If different properties are to be collected depending on the chosen filetype, /// then SetCollectedProperties can be called in response to CommonFileDialog::FileTypeChanged event. /// Note: By default, no properties are collected in the save dialog. /// </remarks> public void SetCollectedPropertyKeys(bool appendDefault, params PropertyKey[] propertyList) { string propertyListStr = null; // Loop through all our property keys and create a semicolon-delimited property list string. if (propertyList != null && propertyList.Length > 0 && propertyList[0] != null) { foreach (PropertyKey key in propertyList) { string canonicalName = ShellPropertyDescriptionsCache.Cache.GetPropertyDescription(key).CanonicalName; // The string we pass to PSGetPropertyDescriptionListFromString must // start with "prop:", followed a list of canonical names for each // property that is to collected. // // Add "prop:" at the start of the string if we are starting our for loop. if (propertyListStr == null) { propertyListStr = "prop:"; } // For each property, append the canonical name, followed by a semicolon if (!string.IsNullOrEmpty(canonicalName)) { propertyListStr += canonicalName + ";"; } } } // If the string was created correctly, get IPropertyDescriptionList for it if (!string.IsNullOrEmpty(propertyListStr)) { Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList); IPropertyDescriptionList propertyDescriptionList = null; try { int hr = PropertySystemNativeMethods.PSGetPropertyDescriptionListFromString(propertyListStr, ref guid, out propertyDescriptionList); // If we get a IPropertyDescriptionList, setit on the native dialog. if (CoreErrorHelper.Succeeded(hr)) { IFileSaveDialog nativeDialog = null; if (nativeDialog == null) { InitializeNativeFileDialog(); nativeDialog = GetNativeFileDialog() as IFileSaveDialog; } if (nativeDialog != null) { hr = nativeDialog.SetCollectedProperties(propertyDescriptionList, appendDefault); if (!CoreErrorHelper.Succeeded(hr)) { Marshal.ThrowExceptionForHR(hr); } } } } finally { if (propertyDescriptionList != null) { Marshal.ReleaseComObject(propertyDescriptionList); } } } }
internal static extern int PSGetPropertyDescriptionListFromString( [In, MarshalAs(UnmanagedType.LPWStr)] string pszPropList, [In] ref Guid riid, out IPropertyDescriptionList ppv );
internal static extern int PSGetPropertyDescriptionListFromString( [In, MarshalAs(UnmanagedType.LPWStr)] string pszPropList, [In] ref Guid riid, out IPropertyDescriptionList ppv );