On .NET Framework you can use System.Windows.Forms.OpenFileDialog
for open files with the native Windows UI but that only works on Windows.
There is a System.Windows.Forms.OpenFileDialog
implementation for .Net Core or another alternative?
On .NET Framework you can use System.Windows.Forms.OpenFileDialog
for open files with the native Windows UI but that only works on Windows.
There is a System.Windows.Forms.OpenFileDialog
implementation for .Net Core or another alternative?
Since .NET Core added support for Windows desktop applications, WPF and Windows Forms applications that target .NET Core can use the OpenFileDialog
and SaveFileDialog
APIs exactly the same way as they would do in the .NET Framework.
These APIs have been ported to the Windows specific desktop packs that sit on top of .NET Core 3 and later.
With new and shiny .NET 5.0 Windows Application using WPF controls it is possible to use OpenFileDialog()
method from Microsoft.Win32
library. It's not identical to Windows Forms version, for example - ShowDialog()
method returns bool?
instead of DialogResult
.
Here is sample MyWpfView.xaml.cs code:
using Microsoft.Win32;
using System.Windows;
namespace TestProject {
public partial class MyWpfView
{
public MyWpfView()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == true)
{
FileTextBox.Text = fileDialog.FileName;
}
}
}
}
.NET Core itself does not include APIs for any UI. For example, https://learn.microsoft.com/en-us/dotnet/api/?view=netcore-2.0&term=File shows nothing relevant. This is also the current stance of .NET Core developers: https://github.com/dotnet/core/issues/374
There are several external libraries that may work on specific platforms:
If you are not really tied to .NET Core itself. UWP (which builds on top of .NET Standard but is different from .NET Core) may be an option too.
Seems like you may just use
<UseWindowsForms>true</UseWindowsForms>
in project file if this app still intended for Windows platform.
I am writing this answer because I see many answers to use Microsoft.Win32
which is basically same thing but you have no shim for backward compatible code.
How to reference System.Windows.Forms in .NET Core 3.0 for WPF apps?