Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Openfiledialog

When I open a file using this code

if (ofd.ShowDialog() == DialogResult.OK)
     text = File.ReadAllText(ofd.FileName, Encoding.Default);

A window appear and ask me to choose file (The File Name is blank as you can see on the image)

enter image description here

If I press second time the button Open to open a file the File Name show the path of the previous selected file (see on image) How I can clear this path every time he press Open button?

enter image description here

like image 363
a1204773 Avatar asked Jun 19 '12 08:06

a1204773


4 Answers

You are probably using the same instance of an OpenFileDialog each time you click the button, which means the previous file name is still stored in the FileName property. You should clear the FileName property before you display the dialog again:

ofd.FileName = String.Empty;
if (ofd.ShowDialog() == DialogResult.OK)
     text = File.ReadAllText(ofd.FileName, Encoding.Default);
like image 77
Marlon Avatar answered Sep 29 '22 13:09

Marlon


try this:

ofd.FileName = String.Empty;
like image 31
Clint Ceballos Avatar answered Sep 29 '22 11:09

Clint Ceballos


You need to reset the filename.

   openFileDialog1.FileName= "";

Or

   openFileDialog1.FileName= String.Empty()
like image 45
Gaz Winter Avatar answered Sep 29 '22 11:09

Gaz Winter


you can simply add this line before calling ShowDialog():

ofd.FileName = String.Empty;
like image 28
John Woo Avatar answered Sep 29 '22 11:09

John Woo