C#, WPF - OpenFileDialog does not appear

前端 未结 7 628
温柔的废话
温柔的废话 2020-12-09 20:54

I have been searching up and down the web and unfortunately never came across an issue quite like mine, so here goes:

My C# WPF application won\'t show me no OpenFil

相关标签:
7条回答
  • 2020-12-09 21:29

    sometime [staThread] not working, you can try this:

        public void showOpenFileDialog()
        {
            OpenFileDialog im = new OpenFileDialog();
            if (im.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = im.FileName;
            }
        }
    
    
        private void select_button_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(showOpenFileDialog));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }
    
    0 讨论(0)
  • 2020-12-09 21:32

    In the console application you will need STAThread appartment for it to work. But WPF is different.

    I would advise you using the File Dialogs only after the window starts and the Main Thread starts working. Try showing your dialog in some MainWindow event of its lifecycle.

    0 讨论(0)
  • 2020-12-09 21:33

    This happened to me recently. The problem was the Main method wasn't marked as an STAThread which will cause the WPF OpenFileDialog's ShowDialog method to block indefinitely.

    static void Main(string[] args)
    {
        var openFileDialog = new OpenFileDialog();
        var result = openFileDialog.ShowDialog();
    }
    

    will never exit or throw an exception, whereas

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

    will work as expected.

    0 讨论(0)
  • 2020-12-09 21:42

    I am experiencing a similar problem, and as Garrett suggested, it is an STA issue. I have been struggling with STA issues a lot in the past few months, as I need to launch screens from a console window (Testing purposes) - this means that the calling thread is not STA, but can be simulated in something like the following:

        [STAThread]
        private void Execute() {
            try {
                Thread t = new Thread(() => {
                    OpenFileDialog dlg = new OpenFileDialog();
                    // The following would not return the dialog if the current
                    // thread is not STA
                    var result = dlg.ShowDialog();
                });
    
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
            } catch (Exception ex) {
                // Handle the exception
                ex.LogException();
            }
        }
    

    Unforunately, it did not work for me to just mark the method as STAThread, I had to launch the operation in a thread marked as STA.

    0 讨论(0)
  • 2020-12-09 21:44

    Not sure if you figured it out or not, but I recently had this same problem. In my case, the problem was that my application hadn't established an existing window.

    My code looked something like this.

    private void Application_Startup(object sender, StartupEventArgs e) {
        string startupFileName = String.Empty();
        if ( startupMode = StartupMode.Load ) {
            // open existing file
            OpenFileDialog openDlg = new OpenFileDialog();
            if (openDlg.ShowDialog() != true)
                 return;
            startupFileName = openDlg.FileName;
        } else {
            // create a new file
            SaveFileDialog saveDlg = new SaveFileDialog();
            if (saveDlg.ShowDialog() != true)
                 return;
            startupFileName = saveDlg.FileName;
        }
    
        // show my main application window
        MainWindow myMainWindow = new MainWindow(startupFileName);
        myMainWindow.Show();
    }
    

    The OpenFileDialog (or SaveFileDialog) would immediately return false without showing because my application had no window for it to attach itself to.

    My solution was to put the open/save code after I created my main window but before I called the Show() method.

    0 讨论(0)
  • 2020-12-09 21:49

    There are a large number of possible failure modes for OpenFileDialog. Using one exposes your app to just about any shell extension that's installed on your machine. Many of which can be very destabilizing, it isn't that likely that the extension author has checked if it works properly in a WPF process.

    Tackle this problem by running SysInternals' AutoRuns utility. Click the Explorer tab and look for the groups that have "ShellEx" in their name. Uncheck anything that wasn't published by Microsoft. Reboot and check if the problem is solved.

    0 讨论(0)
提交回复
热议问题