Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenFileDialog on .NET Core

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?

like image 291
Vinicius Gualberto Avatar asked Oct 17 '17 16:10

Vinicius Gualberto


People also ask

What is OpenFileDialog?

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.


Video Answer


2 Answers

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.

like image 174
mm8 Avatar answered Sep 29 '22 13:09

mm8


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;
            }
        }
    }
}
like image 22
Mark Avatar answered Sep 29 '22 13:09

Mark