@@ -113,32 +113,27 @@ namespace PickerCommon {
113
113
return winrt::hstring{ filePath.get () };
114
114
}
115
115
116
- std::pair< winrt::com_ptr<IShellItem>, std::wstring> ParseFolderItemAndFileName (winrt::hstring const & filePath )
116
+ winrt::com_ptr<IShellItem> TryParseFolderItem (winrt::hstring const & folderPathStr )
117
117
{
118
- std::filesystem::path path (filePath .c_str ());
119
- if (path .empty ())
118
+ std::filesystem::path folderPath (folderPathStr .c_str ());
119
+ if (folderPath .empty ())
120
120
{
121
- return { nullptr , L" " } ;
121
+ return nullptr ;
122
122
}
123
123
124
- auto fileName = path.filename ().wstring ();
125
-
126
- // If the parent folder does not exist or is not a directory, we cannot set folder.
127
- auto folderPath = path.parent_path ();
128
124
if (!std::filesystem::exists (folderPath) || !std::filesystem::is_directory (folderPath))
129
125
{
130
- return { nullptr , fileName } ;
126
+ return nullptr ;
131
127
}
132
128
133
129
winrt::com_ptr<IShellItem> shellItem;
134
130
HRESULT hr = SHCreateItemFromParsingName (folderPath.c_str (), nullptr , IID_PPV_ARGS (shellItem.put ()));
135
131
if (SUCCEEDED (hr))
136
132
{
137
- return { shellItem, fileName } ;
133
+ return shellItem;
138
134
}
139
135
140
- // If the we cannot set the folder, we can at least set the file name suggested by developer.
141
- return { nullptr , fileName};
136
+ return nullptr ;
142
137
}
143
138
144
139
void ValidateViewMode (winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const & value)
@@ -229,7 +224,7 @@ namespace PickerCommon {
229
224
ValidateStringNoEmbeddedNulls (suggestedFileName);
230
225
}
231
226
232
- void ValidateSuggestedSaveFilePath (winrt::hstring const & path)
227
+ void ValidateSuggestedFolder (winrt::hstring const & path)
233
228
{
234
229
if (path.empty ())
235
230
{
@@ -239,25 +234,16 @@ namespace PickerCommon {
239
234
240
235
ValidateStringNoEmbeddedNulls (path);
241
236
242
- wil::unique_cotaskmem_ptr<ITEMIDLIST> pidl (SHSimpleIDListFromPath (path.c_str ()));
243
- if (!pidl)
244
- {
245
- throw std::invalid_argument (" SuggestedSaveFilePath" );
246
- }
247
-
248
- std::filesystem::path p (path.c_str ());
249
- auto folderPath = p.parent_path ();
250
- if (folderPath.empty ())
237
+ auto pathObj = std::filesystem::path (path.c_str ());
238
+ if (!pathObj.is_absolute ())
251
239
{
252
- // If the path does not have a parent, we cannot set folder.
253
- throw std::invalid_argument (" SuggestedSaveFilePath" );
240
+ throw std::invalid_argument (" SuggestedFolder" );
254
241
}
255
242
256
- auto fileName = p. filename (). wstring ( );
257
- if (fileName. size () > MAX_PATH )
243
+ wil::unique_cotaskmem_ptr<ITEMIDLIST> pidl ( SHSimpleIDListFromPath (path. c_str ()) );
244
+ if (!pidl )
258
245
{
259
- throw winrt::hresult_invalid_argument (
260
- PickerLocalization::GetStoragePickersLocalizationText (MaxSaveFileLengthExceededLocalizationKey));
246
+ throw std::invalid_argument (" SuggestedFolder" );
261
247
}
262
248
}
263
249
@@ -404,28 +390,18 @@ namespace PickerCommon {
404
390
// / <param name="dialog"></param>
405
391
void PickerParameters::ConfigureFileSaveDialog (winrt::com_ptr<IFileSaveDialog> dialog)
406
392
{
407
- winrt::hstring fileNameToSet;
408
393
if (!IsHStringNullOrEmpty (SuggestedFileName))
409
394
{
410
- fileNameToSet = SuggestedFileName;
395
+ check_hresult (dialog-> SetFileName ( SuggestedFileName. c_str ())) ;
411
396
}
412
-
413
- if (!PickerCommon::IsHStringNullOrEmpty (SuggestedSaveFilePath ))
397
+
398
+ if (!PickerCommon::IsHStringNullOrEmpty (SuggestedFolder ))
414
399
{
415
- auto result = ParseFolderItemAndFileName (SuggestedSaveFilePath);
416
- winrt::com_ptr<IShellItem> folderItem = result.first ;
417
- fileNameToSet = result.second ;
400
+ winrt::com_ptr<IShellItem> folderItem = TryParseFolderItem (SuggestedFolder);
418
401
if (folderItem)
419
402
{
420
403
check_hresult (dialog->SetFolder (folderItem.get ()));
421
404
}
422
405
}
423
-
424
- // Set the filename (either from SuggestedSaveFilePath or SuggestedFileName)
425
- if (!PickerCommon::IsHStringNullOrEmpty (fileNameToSet))
426
- {
427
- check_hresult (dialog->SetFileName (fileNameToSet.c_str ()));
428
- }
429
-
430
406
}
431
407
}
0 commit comments