file selector c# uwp code example
Example 1: uwp file open picker
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
if (file) {
// Application now has read/write access to the picked file
WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
} else {
// The picker was dismissed with no selected file
WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
}
});
Example 2: can uwp use openfile
public async Task GetFile()
{
Windows.Storage.Pickers.FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
picker.FileTypeFilter.Add(".txt");
//Prompt the user to open the log file:
Windows.Storage.StorageFile logFile = await picker.PickSingleFileAsync();
try
{
using (var stream = await logFile.OpenStreamForReadAsync())
using (var reader = new StreamReader(stream))
{
var line = await reader.ReadLineAsync();
Debug.WriteLine($"The first line: {line} - waiting");
await Task.Delay(10000);
line = await reader.ReadLineAsync();
Debug.WriteLine($"The next line: {line} - waiting");
}
}
catch (Exception exc)
{
Debug.WriteLine($"Exception {exc.Message}");
}
}