How to use OpenFileDialog in WinRT C# Application?

csharp
uwp

(SAM) #1

I’m new to WinRT and UWP, I am using Visual Studio 2015, to develop some Windows WinRT Application for Desktop only, but I couldn’t find OpenFileDialog in WinRT as in .NET and WPF, I’m I missing any dependency or reference?

Thanks


(afree) #2

There is no OpenFileDialog in WinRT. You should use FileOpenPicker instead.

Trying to use the OpenFileDialog is really a very common mistake that UWP newbies commit especially those coming from .NET Framework.

I can’t deny that UWP is a .NET technology that targets .NET core itself. But it doesn’t target .Net Framework in its entirety. OpenFileDialog is part of System.Windows.Forms and it’s not part of .Net Core libraries. You should use FileOpenPicker instead for Windows Store Apps.


(this.is.sparta) #3

you should use FileOpenPicker.

here is an example:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
 
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    OutputTextBlock.Text = "Picked photo: " + file.Name;
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}

Take a look at this article for more information on the subject:


(Sine) #4

There is no OpenFileDialog in Windows Runtime WinRT, but it has FileOpenPicker use it, it’s much better.

As a general rule:

WinRT supports .NET framework but doesn’t support .NET Core.

OpenFileDialog is part of the .Net Core, thus it’s not supported. Use the alternative FileOpenPicker.


(Kunni Can) #5

You must use FileOpenPicker, And By the way that’s a normal confusion that happens to new comers to the WinRT and UWP technology (especially if they have previous WPF/Windows Forms background),

You should understand that there is a big difference between WPF, Windows Forms and WinRT and UWP, This Answer by James on this question explains them all in a really brief and straightforward manner:

As for your question above, as others already answered this line of code should do the trick:

FileOpenPicker openPicker = new FileOpenPicker();