user8977483
user8977483

Reputation: 147

how to open 'New File Dialog' by using FileOpenPicker? (UWP)

I coded like this,

however, this supports only existed file.

I want 'New File Dialog', this means it can get a path though i input non-exist filename.

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
        openPicker.FileTypeFilter.Add(".csv");

        StorageFile file = await openPicker.PickSingleFileAsync();

        if (file != null)
        {

        }
        else
        {

        }

Upvotes: 2

Views: 2390

Answers (2)

B3W
B3W

Reputation: 166

Is there a reason you do not want to use the FileSavePicker class. Here's an example from the MSDN documentation

var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
    // Prevent updates to the remote version of the file until
    // we finish making changes and call CompleteUpdatesAsync.
    Windows.Storage.CachedFileManager.DeferUpdates(file);
    // write to file
    await Windows.Storage.FileIO.WriteTextAsync(file, file.Name);
    // Let Windows know that we're finished changing the file so
    // the other app can update the remote version of the file.
    // Completing updates may require Windows to ask for user input.
    Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
    if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
    {
        this.textBlock.Text = "File " + file.Name + " was saved.";
    }
    else
    {
        this.textBlock.Text = "File " + file.Name + " couldn't be saved.";
    }
}
else
{
    this.textBlock.Text = "Operation cancelled.";
}

Upvotes: 2

Dante May Code
Dante May Code

Reputation: 11247

I think you are looking for FileSavePicker rather than FileOpenPicker.

The syntax is quite similar:

StorageFile file = await savePicker.PickSaveFileAsync();

Upvotes: 3

Related Questions