.Net Standard Microsoft.Win32 does not contain OpenFileDialog() Method

Viewed 322

I am trying to open the file dialog in my WPF application which is a .Net Core app and uses a .Net Standard Library where all my code lives. However, when I tried the OpenFileDialog() method which inherits from Microsoft.Win32 namespace, it says "the name OpenDialog does not exist in the current context" and that is the recurring error even after adding the namespace manually. However. when I tried this in my .netcoreapp, it works just fine. Bringing me to the conclusion that this is probably a problem with .netstandard only. My question is what is the best fix to this, I cannot put the code in my .NetCore WPF app, so the only other alternative is to create a new project for the sole purpose of opening the file dialog which I find a bit too much. Please does anyone have any alternatives, or perhaps I am doing things wrong. I would appreciate all your contributions.

1 Answers

Microsoft.Win32.OpenFileDialog is a Windows-specific API that is not part of .NET Standard.

You could create another class library that targets netcoreapp3.1 or net5.0-windows and then reference your existing .NET Standard class library from this one, e.g.:

WPF App->netcoreapp3.1 class library->netstandard2.0 class library

The other option is to use the Windows Compatibility Pack as an extension to your .NET Standard project.

Related