0
private void button1_Click(object sender, EventArgs e)
    {


        OpenFileDialog newOpen = new OpenFileDialog();

       DialogResult result = newOpen.ShowDialog();

       this.textBox1.Text = result + "";

    }

It just returns "OK"

What am I doing wrong? I wish to get the PATH to the file and display it in a text box.

0

4 Answers 4

1

The ShowDialog method returns whether the user pressed OK or Cancel. This is useful information, but the actual filename is stored as a property on the dialog

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog newOpen = new OpenFileDialog();
        DialogResult result = newOpen.ShowDialog();
        if(result == DialogResult.OK) {
              this.textBox1.Text = newOpen.FileName;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You need to access the filename:

 string filename = newOpen.FileName;

or filenames, if you allowed multiple file selection:

newOpen.FileNames;

Ref.: OpenFileDialog Class

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)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file. Error: " + ex.Message);
        }
    } 
}

Comments

0

You need to read the FileName property of the OpenFileDialog instance. This will get you the path of the selected file.

Comments

0

Here is an example of using an existing file as a default, and getting a new file back:

private string open(string oldFile)
    {
        OpenFileDialog newOpen = new OpenFileDialog();
        if (!string.IsNullOrEmpty(oldFile))
        {
          newOpen.InitialDirectory = Path.GetDirectoryName(oldFile);
          newOpen.FileName = Path.GetFileName(oldFile);
        }

        newOpen.Filter = "eXtensible Markup Language File  (*.xml) |*.xml"; //Optional filter
        DialogResult result = newOpen.ShowDialog();
        if(result == DialogResult.OK) {
              return newOpen.FileName;
        }

        return string.Empty;
    }

Path.GetDirectoryName(file) : Return path

Path.GetFileName(file) : Return filename

Comments

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.