0

I try to open System file dialog to select a pic. the code ran normally in my computer. But in another computer cant show the system file dialog.

And here is my simple code:-

private void PicInputBtn_Click(object sender, RoutedEventArgs e)
{
    var dialog = new Microsoft.Win32.OpenFileDialog
    {
        DefaultExt = ".jpg",
        Filter = "img file|*.jpg",
    };

    if (dialog.ShowDialog() != true)
    {
        return;
    }
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
Fireyu
  • 1
  • 1

2 Answers2

1

If nothing happens but the mouse pointer turning into a little busy-indicator.

You can try to set your thread to STAThread:

[STAThread]    
static void Main(string[] args)
{
    var o = new OpenFileDialog();
    var r = o .ShowDialog();
}

Howover they are many reasons that can break the OpenFileDialog, you can try to launch your program in admin mode and try to reinstall .net Framework

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
0

Change your code to:

private void PicInputBtn_Click(object sender, RoutedEventArgs e)
{
    var dialog = new Microsoft.Win32.OpenFileDialog
    {
        DefaultExt = ".jpg",
        Filter = "img file|*.jpg" // You had an extra ',' here.
    };

    if ((Nullable<bool>dialog.ShowDialog()) == true) // Also you forgot to put Nullable<bool>
    {
        // string filename = dlg.FileName;
    }
    else
    {
        return;
    }
}
  • Maybe not the code question,I ask my teammate to download the win7 sp1 patch and her computer can show the OpenFileDialog correctly! anyway,still thanks for your anwser!!! – Fireyu Jun 29 '16 at 09:39
  • @Fireyu That's okay. –  Jun 29 '16 at 09:42