How to show OpenFileDialog in RoslynPad

348 views Asked by At

I using following code to show OpenFileDialog using RoslynPad, it complied and run, but no dialog appear, so the snippet keep running forever:

#r "framework:Microsoft.WindowsDesktop.App"

using System.Windows.Forms;

var fd = new OpenFileDialog
{
    Filter = "Solution files (*.sln)|*.sln"
};

if (fd.ShowDialog() == DialogResult.OK)
    Console.WriteLine(fd.FileName);

What is correct way to make OpenFileDialog work with RoslynPad?

Environment:

  • OS: Windows 10 Pro 64 bit (2004)
  • RoslynPad: built from lastest master branch.
  • .NET Core: 3.1.402
1

There are 1 answers

1
Horizon On

After checking the repo, I'm able to make OpenFileDialog work by adding below line:

await Helpers.RunWpfAsync();

The complete code is as below:

#r "framework:Microsoft.WindowsDesktop.App"

using Microsoft.Win32;

await Helpers.RunWpfAsync(); // initializes a dispatcher thread

var fd = new OpenFileDialog
{
    Filter = "Solution files (*.sln)|*.sln"
};

if (fd.ShowDialog() == true)
    Console.WriteLine(fd.FileName);

RunWpfAsync creates a WPF Dispatcher thread and the code following the await runs under it. It will also work for Windows Forms.