1

I am trying to open a file by pressing a button (a label to be more exact but it works just the same way)

For some reason when the FileDialog opens and I select the file and press open it doesnt open the file it only closes the FileDialog

private void selectLbl_Click(object sender, EventArgs e)
{

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.InitialDirectory = "c:\\";
    ofd.Filter = "Script files (*.au3)|*.au3";
    ofd.RestoreDirectory = true;
    ofd.Title = ("Select Your Helping Script");


    if (ofd.ShowDialog() == DialogResult.OK)
    {
        ofd.OpenFile(); //Not sure if there is supposed to be more here
    }
}
5
  • 4
    yes.. because it returns you the path of the file - its then up to you what you do with it. Commented May 17, 2016 at 11:57
  • You're misunderstanding the purpose of the FileOpenDialog. Commented May 17, 2016 at 11:58
  • This guide might help you understand what OpenFileDialog does Commented May 17, 2016 at 11:59
  • 1
    ofd.OpenFile(); returns a Stream to allow the program to read the file, not to open the file itself for the user to see Commented May 17, 2016 at 12:02
  • What do you expect to happen after you selected a file? Commented May 17, 2016 at 12:05

3 Answers 3

3
ofd.OpenFile();

is returning the content of the file as Stream of bytes like described here. If you want to open the file like you described it, use

if (ofd.ShowDialog() == DialogResult.OK)
{
  System.Diagnostics.Process.Start(ofd.FileName);
}

So your selected file starts with the associated application.

Sign up to request clarification or add additional context in comments.

Comments

1

ofd.OpenFile() opens the file selected by the user as a Stream that you can use to read from the file.

What you do with that stream depends on what you are trying to achieve. For example, you can read and output all lines:

if (ofd.ShowDialog() == DialogResult.OK)
{
    using (TextReader reader = new StreamReader(ofd.OpenFile()))
    {
        string line;
        while((line = t.ReadLine()) != null)
            Console.WriteLine(line);
    }
}

Or if it is an xml file you can parse it as xml:

if (ofd.ShowDialog() == DialogResult.OK)
{
    using(XmlTextReader t = new XmlTextReader(ofd.OpenFile()))
    {
        while (t.Read())
            Console.WriteLine($"{t.Name}: {t.Value}");
    }
}

Comments

0

The OpenFileDialog is not a dialog that opens the file. It only communicates with the operator with a dialog that is commonly shown if a program needs to know what file to open. So it is only a dialog box, not a file opener.

You decide what to do if the user pressed ok or cancel:

private void selectLbl_click(object sender, ...)
{
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        ofd.InitialDirectory = "c:\\";
        ofd.Filter = "Script files (*.au3)|*.au3";
        ofd.RestoreDirectory = true;
        ofd.Title = ("Select Your Helping Script");

        var dlgResult = ofd.ShowDialog(this);
        if (dlgResult == DialogResult.OK)
        {    // operator pressed OK, get the filename:
             string fullFileName = ofd.FileName;
             ProcessFile(fullFileName);
        }
    }
}


if (ofd.ShowDialog() == DialogResult.OK)
{
    ofd.OpenFile(); //Not sure if there is supposed to be more here
}

1 Comment

"The OpenFileDialog is not a dialog that opens the file". That is not true, OpenFile does open the file, but returns an opened file stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.