Recently, I'm developing a tool with WinUI, but now I view a problem when using FileOpenPicker
throw exception In Administrator model, you guys can reproduce this problem with following code.
private async void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
// See the sample code below for how to make the window accessible from the App class.
var window = App.Current.m_window;
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
// Initialize the file picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd);
// Set options for your file picker
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.FileTypeFilter.Add("*");
// Open the picker for the user to pick a file
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
await Windows.Storage.FileIO.ReadTextAsync(file);
}
else
{
throw new Exception("file is null");
}
}
For making sure the app run in Administrator, please edit project profile like this link