Ashish Sharma February 2016

OpenFileDialog is not opening the file

I have bound the openFileDialog control with button. On button event, I am calling the openFileDialog control.

Now, when I am running my application, the openFileDialog box gets open but does not select the file. The open button is not working.

Screen capture of OpenFileDialog

Example code:

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

    }

    private void openFileDialog1_FileOk_1(object sender, CancelEventArgs e)
    {
        // logic written here
    }

It was working fine earlier but now its not working.

Answers


Gnqz February 2016

You need to use the DialogResult to get the event of open confirmation by the user. Then you can use a stream to read the file. Here is some sample code (provided by MS in the MSDN - source:https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx):

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Some logic here
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Failed to open file. Original error: " + ex.Message);
        }
    }
}


Theraot February 2016


Answer post edits

The included code shows the method openFileDialog1_FileOk_1 the "_1" at the end suggest to me that you had some issues binding the event. Perhaps there was at some point a method openFileDialog1_FileOk caused conflict.

You should check if the method is correctly bound to the event.

For that I will remit you to my answer to How to change the name of an existing event handler?

For abstract you want to see what method is bound to the event. You can do it from the properties panel, or by checking the form designer file (that is named something .Designer.cs for example: Form1.Designer.cs).

Addendum: Consider adding a break point in the event handler. It will allow you to debug step by step what is happening. Also, it will allow you notice if the event handlers is not being executed at all, which would suggest that the method is NOT correctly bound to the event.


Original Answer

OpenFileDialog Does not open files, it barely select thems and makes the selection available for your application to do whatever your applications is intended to do with those files.

The following is the usage pattern from the MSDN article linked above:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show
        (
            "Error: Could not read file from disk. Original error: " + ex.Message
        );
    }
}

Now, observe that after cheking the resu


Abdur Rahim February 2016

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string name = openFileDialog1.FileName;
object objOpt = Type.Missing;
                var excelApp = new Excel.Application();
                Excel.Workbook wbclass = excelApp.Workbooks.Open(name, objOpt, 
                                                                true,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt);

            }

Post Status

Asked in February 2016
Viewed 1,077 times
Voted 14
Answered 3 times

Search




Leave an answer