2 using System
.Collections
.Generic
;
3 using System
.Diagnostics
;
4 using System
.Runtime
.InteropServices
;
6 using Microsoft
.SDK
.Samples
.VistaBridge
.Library
;
7 using Microsoft
.SDK
.Samples
.VistaBridge
.Interop
;
8 using System
.Security
.Permissions
;
10 namespace Microsoft
.SDK
.Samples
.VistaBridge
.Library
13 /// Provides access to a Vista Common File Dialog, which allows the user
14 /// to select the filename and location for a saved file.
16 /// <permission cref="System.Security.Permissions.FileDialogPermission">
17 /// to save a file. Associated enumeration: <see cref="System.Security.Permissions.SecurityAction.LinkDemand"/>.
19 [FileDialogPermissionAttribute(SecurityAction
.LinkDemand
, Save
= true)]
20 public sealed class CommonSaveFileDialog
: CommonFileDialog
22 private NativeFileSaveDialog saveDialogCoClass
;
25 /// Creates a new instance of this class.
27 public CommonSaveFileDialog() : base() { }
29 /// Creates a new instance of this class with the specified name.
31 /// <param name="name">The name for this dialog.</param>
32 public CommonSaveFileDialog(string name
) : base(name
) { }
34 #region Public API specific to Save
36 private bool overwritePrompt
;
39 /// Gets or sets a value that controls whether to prompt before
40 /// overwriting an existing file of the same name.
42 /// <permission cref="System.InvalidOperationException">
43 /// This property cannot be changed when the dialog is showing.
45 public bool OverwritePrompt
47 get { return overwritePrompt; }
50 ThrowIfDialogShowing("OverwritePrompt" + IllegalPropertyChangeString
);
51 overwritePrompt
= value;
55 private bool createPrompt
;
57 /// Gets or sets a value that controls whether to prompt for creation
58 /// if the item returned in the save dialog does not exist.
60 /// <remarks>Note that this does not actually create the item.</remarks>
61 /// <permission cref="System.InvalidOperationException">
62 /// This property cannot be changed when the dialog is showing.
64 public bool CreatePrompt
66 get { return createPrompt; }
69 ThrowIfDialogShowing("CreatePrompt" + IllegalPropertyChangeString
);
74 private bool enableMiniMode
;
76 /// Gets or sets a value that controls whether to the save dialog
77 /// displays in expanded mode.
79 /// <remarks>Expanded mode controls whether the dialog
80 /// shows folders for browsing or hides them.</remarks>
81 /// <permission cref="System.InvalidOperationException">
82 /// This property cannot be changed when the dialog is showing.
84 public bool EnableMiniMode
86 get { return enableMiniMode; }
89 ThrowIfDialogShowing("EnableMiniMode" + IllegalPropertyChangeString
);
90 enableMiniMode
= value;
94 private bool strictExtensions
;
96 /// Gets or sets a value that controls whether to ensure that the
97 /// returned file name has a file extension that matches the
98 /// currently selected file type. The dialog appends the correct
99 /// file extension if necessary.
101 /// <permission cref="System.InvalidOperationException">
102 /// This property cannot be changed when the dialog is showing.
104 public bool StrictExtensions
106 get { return strictExtensions; }
109 ThrowIfDialogShowing("StrictExtensions" + IllegalPropertyChangeString
);
110 strictExtensions
= value;
116 internal override void InitializeNativeFileDialog()
118 saveDialogCoClass
= new NativeFileSaveDialog();
121 internal override IFileDialog
GetNativeFileDialog()
123 Debug
.Assert(saveDialogCoClass
!= null,
124 "Must call Initialize() before fetching dialog interface");
125 return (IFileDialog
)saveDialogCoClass
;
128 internal override void PopulateWithFileNames(
129 System
.Collections
.ObjectModel
.Collection
<string> names
)
132 saveDialogCoClass
.GetResult(out item
);
135 throw new InvalidOperationException(
136 "Retrieved a null shell item from dialog");
137 names
.Add(GetFileNameFromShellItem(item
));
140 internal override void CleanUpNativeFileDialog()
142 if (saveDialogCoClass
!= null)
143 Marshal
.ReleaseComObject(saveDialogCoClass
);
146 internal override SafeNativeMethods
.FOS
GetDerivedOptionFlags(SafeNativeMethods
.FOS flags
)
149 flags
|= SafeNativeMethods
.FOS
.FOS_OVERWRITEPROMPT
;
151 flags
|= SafeNativeMethods
.FOS
.FOS_CREATEPROMPT
;
153 flags
|= SafeNativeMethods
.FOS
.FOS_DEFAULTNOMINIMODE
;
154 if (strictExtensions
)
155 flags
|= SafeNativeMethods
.FOS
.FOS_STRICTFILETYPES
;