0

I have a FileDialog...

    string fileData = openFileDialog1.FileName;

...and a TextBox1. How to see the full path of the opened file in the TextBox1?

Solution:

        textBox1.Text = string.Format("{0}", openFileDialog1.FileName);
user922907
  • 539
  • 2
  • 8
  • 18
  • as I know it should be full path of the file. Can you describe in details? `if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.textBox1.Text = this.openFileDialog1.FileName; }` – Samich Sep 04 '11 at 10:40
  • 1
    horrible mess in this question, requirements are not clear and no real useful code sample, then question was edited with solution which is wrong because initially there was no openFileDialog2 and anyway answers below already said how to do it, string.format above is useless. – Davide Piras Sep 04 '11 at 22:32

6 Answers6

6

using TextBox1.Text = openFileDialog1.FileName;

Waqas
  • 6,812
  • 2
  • 33
  • 50
2

this is the best code it works 100% for me :

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PDF Files(*.pdf)|*.pdf|WORD Files(*.doc;*.docx)|*.doc;*.docx|EXCEL Files(*.xlsx;*.xlsm;*.xlsb;*.xltx;*.xltm;*.xls;*.xlt)|*.xlsx;*.xlsm;*.xlsb;*.xltx;*.xltm;*.xls;*.xlt|Image Files(*.jpg;*.gif;*.bmp;*.png;*.jpeg)|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            string path = ofd.FileName.ToString();
            textBox1.Text = path;
        }
nassimlouchani
  • 423
  • 4
  • 8
1

this should work:

TextBox1.Text = openFileDialog1.FileName;

if does not work, please refine your question telling exactly what you need to retrieve and giving examples.

you might want to check this one as well:

Extracting Path from OpenFileDialog path/filename

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

see below code.

TextBox1.Text = string.Format("{0}/{1}",
    Path.GetDirectoryName(fileData),openFileDialog1.FileName);
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
Henry
  • 1,010
  • 8
  • 10
0

After declaring variable, try this:

String filePath = openFileDialog1.FileName;
textbox1.Text = filePath;
mmking
  • 1,564
  • 4
  • 26
  • 36
0

You may also use TextBox1.Text = fileUpload.PostedFile.FileName; depending upon when you want to access the information.

McArthey
  • 1,614
  • 30
  • 62