Skip to content

Proposal to expose FileDialog.ClientGuid #2858

Closed

Description

@jnm2

What it is

From docs: IFileDialog::SetClientGuid enables a calling application to associate a GUID with a dialog's persisted state. A dialog's state can include factors such as the last visited folder and the position and size of the dialog. Typically, this state is persisted based on the name of the executable file. By specifying a GUID, an application can have different persisted states for different versions of the dialog within the same application (for example, an import dialog and an open dialog).

Exposing IFileDialog::SetClientGuid is in line with one of the aims which the contributing guide calls out for .NET 5.0 and beyond:

[U]pdate implementations to deliver all aspects of modern Windows UI, including missings properties or actions

API proposal

Draft implementation: #2859

namespace System.Windows.Forms
{
    public abstract partial class FileDialog : CommonDialog
    {
        // Public instance properties
        public bool AddExtension { get; set; }
        public virtual bool CheckFileExists { get; set; }
        public bool CheckPathExists { get; set; }
+       public System.Guid? ClientGuid { get; set; }
        public string DefaultExt { get; set; }
        public bool DereferenceLinks { get; set; }
        public string FileName { get; set; }
        public string[] FileNames { get; }
        public string Filter { get; set; }
        public int FilterIndex { get; set; }
        public string InitialDirectory { get; set; }
        public bool RestoreDirectory { get; set; }
        public bool ShowHelp { get; set; }
        public bool SupportMultiDottedExtensions { get; set; }
        public bool Title { get; set; }
        public bool ValidateNames { get; set; }
    }
}

Lack of alternatives

We were recently in a situation where the use case fit the SetClientGuid concept perfectly. In our plugin for an application which is not published by us, our file dialogs needed to track and open to a last-used import directory without affecting the initial directory of the existing file dialogs across the base application. The base application doesn't set the initial directory when it shows a file dialog. Windows shares the state of all dialogs which have no client GUID across the whole app. The only way to keep our dialogs from overwriting the last-used directory for all the dialogs across the base application was to use SetClientGuid.

Because Windows Forms does not expose this capability, nor does it even provide a hook we could use to obtain the object instance to make COM calls on, I ended up taking a few hours and implementing our own file dialog library with a ClientGuid property.

📝 If we were in a situation where we had control over the code in the base application that showed file dialogs, one alternative would be to save and restore FileDialog.InitialDirectory to two separate storage locations. This feels more like a hack than idiomatic use of the Windows file dialog feature.

Template questions

Will this feature affect UI controls?

Yes, Windows saves and restores initial directory and dialog screen position with the application name as the key. This feature would enable opting in to using an arbitrary GUID as the key instead of the application name.

Will VS Designer need to support the feature? If yes, describe how you expect it to funсtion.

No, [Browsable(false)] should be fine. On the other hand, the property grid's default behavior for a property of type System.Guid? might be desirable. We could even be fancy and add an editor button that sets the property to Guid.NewGuid().

What impact will it have on accessibility?

None.

Will this feature need to be localized or be localizable?

Just the SRDescriptionAttribute on the new property, I think? At runtime, no.

Activity

  1. added
    api-suggestion
    on Feb 15, 2020
  2. RussKie commented on Feb 18, 2020

    @RussKie
    Contributor

    I think it totally makes sense to expose a property 👍

    Just worth noting that this functionality will only be available for a Vista-style dialog that is based on IFileDialog. Otherwise an old-style dialog is invoked via GetOpenFileNameW and GetSaveFileNameW API.

    if (UseVistaDialogInternal && TryRunDialogVista(hWndOwner, out bool returnValue))
    {
    return returnValue;
    }
    return RunDialogOld(hWndOwner);

  3. added
    api-ready-for-review
    on Feb 18, 2020
  4. added
    api-approved
    and removed
    api-ready-for-review
    on Mar 5, 2020
  5. terrajobst commented on Mar 5, 2020

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System.Windows.Forms
    {
        public partial class FileDialog
        {
            public System.Guid? ClientGuid { get; set; }
        }
    }
  6. added this to the 5.0 milestone on Mar 6, 2020
  7. ghost added
    🚧 work in progress
    on Mar 6, 2020
  8. ghost removed
    🚧 work in progress
    on Mar 10, 2020
  9. removed this from the 5.0 Preview 1- 4 milestone on Apr 20, 2020
  10. ghost locked as resolved and limited conversation to collaborators on Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedapi-suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions