WinUI FileOpenPicker Throw Exception When Run Application In Administrator

Nico 126 Reputation points
2024-08-06T07:17:53.8266667+00:00

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

Universal Windows Platform (UWP)
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
794 questions
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 18,486 Reputation points Microsoft Vendor
    2024-08-06T09:22:40.32+00:00

    Hi @Nico , Welcome to Microsoft Q&A!

    This is indeed an unexpected behavior. Someone has already posted this unexpected behavior on GitHub https://github.com/microsoft/WindowsAppSDK/issues/2504.It is recommended to post this issue on GitHub.

    Thank you.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.