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?
OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.
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;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With