2

How can I get my FileSavePicker to open in the same folder that was picked by th...

 2 years ago
source link: https://devblogs.microsoft.com/oldnewthing/20220525-00/?p=106685
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How can I get my FileSavePicker to open in the same folder that was picked by the FileOpenPicker or FolderPicker?

RaymondChen_5in-150x150.jpg

Raymond Chen

May 25th, 20221

Say you want your File­Save­Picker to default to the same folder that was picked by the Folder­Picker. Or your File­Open­Picker to default to the same directory that was used by the FileSavePicker. Or any combination of the above. Basically, you want all the pickers to resume where the previous one left off. How do you do that?

By default, each picker keeps a separate history of recent locations, but you can override this by setting an explicit Settings­Identifier on the picker. We saw this earlier when we explored how to keep two different sets of history, so that you could have a most recent location for, say, movie clips, and a different most recent location for background music.

But in addition to keeping settings separate, you can use the Settings­Identifier to make them the same.

If you give your File­Save­Picker, File­Open­Picker, and Folder­Picker the same Settings­Identifier, then they will share the same history of recent locations. Each one will resume in the location that the previous one left off.

async Task<StorageFile> LoadAsync()
{
  var picker = new FileOpenPicker {
    SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
    FileTypeFilter = { ".txt" },
    SettingsIdentifier = "Common"
  };
  return await picker.PickSingleFileAsync();
}

async Task<StorageFile> SaveAsync()
{
  var picker = new FileSavePicker {
    SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
    FileTypeChoices = { ["Plain Text"] = new[] { ".txt" } },
    SuggestedFileName = "New Document",
    SettingsIdentifier = "Common"
  };
  return await picker.PickSaveFileAsync();
}

async Task<StorageFolder> PickFolderAsync()
{
  var picker = new FolderPicker {
    SuggestedStartLocation = PickerLocationId.VideosLibrary,
    FileTypeFilter = { ".txt" },
    SettingsIdentifier = "Common"
  };
  return await picker.PickSingleFolderAsync();
}

You can combine this with the previous trick of keeping different pickers separate: You can have a group of pickers that all share their settings for movie clips, and another group of pickers that share their settings for background music.

Bonus chatter: The Win32 equivalent of the Windows Runtime SettingsIdentifier is IFileDialog::SetClientGuid. Call IFileDialog::ClearClientData to clean up the saved information.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK