10

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?

phoenix
  • 7,988
  • 6
  • 39
  • 45
Vinicius Gualberto
  • 599
  • 1
  • 6
  • 15
  • 5
    .NET Core codebase has intentionally left out all the platform specific APIs (including WinForms/WPF and any other UI element). You may want to try some cross-platform alternative like Gtk# and maybe Qt or Xamarin.Forms (not sure about the last one). Even better, keep your non ui code in a .NET Standard project and reference it from several platform specific UI project. – Stefano d'Antonio Oct 17 '17 at 16:40

4 Answers4

17

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    Do you know how to set the `InitialDirectory` for an `OpenFileDialog` in a Windows Form Application that is running on Linux? The file browser works but it doesn't seem to accept any initial directory using either `\` or `/` – Danny Jan 11 '22 at 17:10
13

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;
            }
        }
    }
}
Mark
  • 1,357
  • 16
  • 30
6

.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.

omajid
  • 14,165
  • 4
  • 47
  • 64
3

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?