Repository navigation
Description
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
RussKie commentedon Feb 18, 2020
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 viaGetOpenFileNameWandGetSaveFileNameWAPI.winforms/src/System.Windows.Forms/src/System/Windows/Forms/FileDialog.cs
Lines 736 to 741 in 2f73fda
terrajobst commentedon Mar 5, 2020
Video