Get filename from OpenFileDialog

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

Hi all,
how can I obtain only the filename with this snippet:

OpenFileDialog dialog = new OpenFileDialog();
string fileName = dialog.FileName;

without the path (C:\etc....)?

Thanks.
 
Luigi said:
Hi all,
how can I obtain only the filename with this snippet:

OpenFileDialog dialog = new OpenFileDialog(); string fileName =
dialog.FileName;

without the path (C:\etc....)?

Thanks.

Have a look at the methods of the System.IO.Path class.


Armin
 
Hi all,
how can I obtain only the filename with this snippet:

OpenFileDialog dialog = new OpenFileDialog();
string fileName = dialog.FileName;

without the path (C:\etc....)?

Thanks.

Hi Luigi,
You can get filename with extension using System.IO.Path.GetFilename
method. It removes the rest of the path including folders and returns
only the filename with extension.

As in your sample, you can call:

OpenFileDialog dialog = new OpenFileDialog();
string fileName = System.IO.Path.GetFileName(dialog.FileName);

If you don't want the extension, you can use
"GetFileNameWithoutExtension" method under Path namespace.

HTH,

Onur Güzel
 
Back
Top