Example #1
0
        /// <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);
                    }
                }
            }
        }
Example #2
0
        /// <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);
                    }
                }
            }
        }