T
T
Test2017-08-02 12:48:59
.NET
Test, 2017-08-02 12:48:59

How to call open file dialog?

From some recent time

private void button1_Click(object sender, EventArgs e)
        {
                 FileDialog.ShowDialog();
        }

Started throwing exceptions
The current thread must be set to a single-threaded container (STA) thread for OLE calls to be possible.

He says to put [STAThread] before Main, which I actually have.
One identical problem was solved by the delegate and the invoke method
private delegate DialogResult ShowSaveFileDialogInvoker();
        private void button1_Click(object sender, EventArgs e)
        {
            ShowSaveFileDialogInvoker invoker = new ShowSaveFileDialogInvoker(FileDialog.ShowDialog);

            this.Invoke(invoker); // where this is a Form
        }

However, this does not solve my problem and the same exception pops up

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
denismaster, 2017-08-02
@denismaster

Try:

var fileDialog = new OpenFileDialog();
if(fileDialog.ShowDialog()==DialogResult.OK) { //code }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question