@@ -39,25 +39,26 @@ using Microsoft.Windows.Storage.Pickers;
39
39
40
40
var savePicker = new FileSavePicker (this .AppWindow .Id )
41
41
{
42
- // (Optional) specify the initial location.
43
- // If not specified, default to PickerLocationId.Unspecified.
42
+ // (Optional) Specify the initial location for the picker.
43
+ // If the specified location doesn't exist on the user's machine, it falls back to the DocumentsLibrary.
44
+ // If not set, it defaults to PickerLocationId.Unspecified, and the system will use its default location.
44
45
SuggestedStartLocation = PickerLocationId .DocumentsLibrary ,
45
46
46
47
// (Optional) specify the default file name. If not specified, use system default.
47
48
SuggestedFileName = " My Document" ,
48
49
49
- // (Optional) specify the text displayed on commit button.
50
- // If not specified, the system uses a default label of "Open " (suitably translated).
50
+ // (Optional) specify the text displayed on the commit button.
51
+ // If not specified, the system uses a default label of "Save " (suitably translated).
51
52
CommitButtonText = " Save Document" ,
52
53
53
- // (Optional) categorized extensions types. If not specified, allow All Files (*.*)
54
- // Note that when allow All Files (*.*), end users can save a file without extension.
54
+ // (Optional) categorized extension types. If not specified, " All Files (*.*)" is allowed.
55
+ // Note that when " All Files (*.*)" is allowed , end users can save a file without an extension.
55
56
FileTypeChoices = {
56
57
{ " Documents" , new List <string > { " .txt" , " .doc" , " .docx" } }
57
58
},
58
59
59
- // (Optional) specify the default file extension (will be appended after the default file name ).
60
- // If not specified, will not appended after the default extension .
60
+ // (Optional) specify the default file extension (will be appended to SuggestedFileName ).
61
+ // If not specified, no extension will be appended .
61
62
DefaultFileExtension = " .txt" ,
62
63
};
63
64
```
@@ -70,37 +71,38 @@ using namespace winrt::Microsoft::Windows::Storage::Pickers;
70
71
71
72
FileSavePicker savePicker(AppWindow().Id());
72
73
73
- // (Optional) specify the initial location.
74
- // If not specified, default to PickerLocationId.Unspecified.
74
+ // (Optional) Specify the initial location for the picker.
75
+ // If the specified location doesn't exist on the user's machine, it falls back to the DocumentsLibrary.
76
+ // If not set, it defaults to PickerLocationId.Unspecified, and the system will use its default location.
75
77
savePicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary);
76
78
77
79
// (Optional) specify the default file name. If not specified, use system default.
78
80
savePicker.SuggestedFileName(L"NewDocument");
79
81
80
- // (Optional) specify the text displayed on commit button.
81
- // If not specified, the system uses a default label of "Open " (suitably translated).
82
+ // (Optional) specify the text displayed on the commit button.
83
+ // If not specified, the system uses a default label of "Save " (suitably translated).
82
84
savePicker.CommitButtonText(L"Save Document");
83
85
84
- // (Optional) categorized extensions types. If not specified, allow All Files (* .* )
85
- // Note that when allow All Files (* .* ), end users can save a file without extension.
86
+ // (Optional) categorized extension types. If not specified, " All Files (* .* )" is allowed.
87
+ // Note that when " All Files (* .* )" is allowed , end users can save a file without an extension.
86
88
savePicker.FileTypeChoices().Insert(L"Text", winrt::single_threaded_vector< winrt::hstring > ({ L".txt" }));
87
89
88
- // (Optional) specify the default file extension (will be appended after the default file name ).
89
- // If not specified, will not appended after the default extension .
90
+ // (Optional) specify the default file extension (will be appended to SuggestedFileName ).
91
+ // If not specified, no extension will be appended .
90
92
savePicker.DefaultFileExtension(L".txt");
91
93
```
92
94
93
95
## Setting the FileSavePicker.SuggestedSaveFilePath Property
94
96
95
- The `SuggestedSaveFilePath` property of `FileSavePicker` dictates 2 key aspects of the save file
97
+ The `SuggestedSaveFilePath` property of `FileSavePicker` dictates two key aspects of the save file
96
98
dialog's initial state:
97
99
98
100
* **Initial Directory:** The dialog defaults to the directory of the `SuggestedSaveFilePath`.
99
101
100
102
This behavior overrides any picker-specific remembered folder settings and ensures the dialog
101
103
opens in the suggested file's parent folder.
102
104
103
- * **Pre-filled File Name:** The file name field in the dialog is pre-populated with the name of
105
+ * **Pre-filled File Name:** The file name field in the dialog is pre-populated with the name from
104
106
the `SuggestedSaveFilePath`.
105
107
106
108
This file name overrides the `FileSavePicker.SuggestedFileName` property if both are set.
111
113
using Microsoft.Windows.Storage.Pickers;
112
114
113
115
var savePicker = new FileSavePicker(this.AppWindow.Id);
114
- savePicker.SuggestedSaveFilePath( @"C:\temp\MyProject\MyFile.txt") ;
116
+ savePicker.SuggestedSaveFilePath = @"C:\temp\MyProject\MyFile.txt";
115
117
```
116
118
117
119
C++
@@ -120,34 +122,42 @@ C++
120
122
using namespace winrt ::Microsoft::Windows::Storage::Pickers;
121
123
122
124
FileSavePicker savePicker(AppWindow().Id());
123
- bool isSuccess = savePicker.SuggestedSaveFilePath(L"C:\\ temp\\ MyProject\\ MyFile.txt");
125
+ savePicker.SuggestedSaveFilePath(L"C:\\ temp\\ MyProject\\ MyFile.txt");
124
126
```
125
127
126
- ### The Parsing Logic of Setting SuggestedSaveFilePath
128
+ ### The Parsing Logic
127
129
128
- `SuggestedSaveFilePath` takes whatever after **the last slash** to fill in the file name box,
129
- as long as the part before the last slash is a valid and existing folder.
130
+ The `SuggestedSaveFilePath` property is parsed to set the directory and file name in the
131
+ save dialog.
130
132
131
- It allows the value to be set to an empty string. However, it raises invalid argument exception
132
- if the non-empty input doesn't contain have a folder path.
133
+ - The substring before the final path separator (`\`) is used as the initial directory.
134
+ This part must be written in the format of a folder path.
135
+ - The substring after the final path separator is used as the suggested file name.
136
+ - If `SuggestedSaveFilePath` is set to an empty string, the property is cleared and will not take
137
+ effect when launching the save dialog.
138
+ - If the provided path is a non-empty string but it does not have a folder path
139
+ (e.g., `"MyFile.txt"`), an `InvalidArgumentException` will be thrown.
133
140
134
- Here're some examples about the parsing logic:
141
+ The following table illustrates the parsing behavior:
142
+
143
+ | `SuggestedSaveFilePath` Input | Resulting Initial Directory | Resulting File Name |
144
+ |-------------------------------|-----------------------------|---------------------|
145
+ | `'C:\temp\MyProject\MyFile.txt'` | `'C:\temp\MyProject'` | `'MyFile.txt'` |
146
+ | `'C:\temp\MyProject\.git\'` | `'C:\temp\MyProject\.git'` | `''` (empty) |
147
+ | `'C:\temp\MyProject\.git'` | `'C:\temp\MyProject'` | `'.git'` |
148
+ | `'C:\servers\www.bing.com\'` | `'C:\servers\www.bing.com'` | `''` (empty) |
149
+ | `'C:\servers\www.bing.com'` | `'C:\servers'` | `'www.bing.com'` |
150
+ | `''` (empty string) | null |
151
+ | `'MyFile.txt'` | Throws `InvalidArgumentException` | Throws `InvalidArgumentException` |
135
152
136
- |Input path string | Set folder | Set file name |
137
- |------------------|------------|---------------|
138
- |`'C:\temp\MyProject\MyFile.txt'`|`'C:\temp\MyProject'`| `'MyFile.txt'`|
139
- |`'C:\temp\MyProject\.git\'`|`'C:\temp\MyProject\.git'`| `''`|
140
- |`'C:\temp\MyProject\.git'`|`'C:\temp\MyProject'`| `'.git'`|
141
- |`'C:\servers\www.bing.com\'`|`'C:\servers\www.bing.com'`| `''`|
142
- |`'C:\servers\www.bing.com'`|`'C:\servers`| `'www.bing.com'`|
143
153
144
154
## FileSavePicker.PickSaveFileAsync
145
155
146
156
Displays a UI element that allows the user to configure the file path to save.
147
157
148
- Returns a light weight object that has the path of the saved file.
158
+ Returns a lightweight object that has the path of the saved file.
149
159
150
- Returns null if the file dialog was cancelled or closed without saved file.
160
+ Returns ` null` if the file dialog was cancelled or closed without saving a file.
151
161
152
162
### Examples
153
163
0 commit comments