getting filename of OpenFileDialog

ganders

Active member
Joined
Nov 25, 2004
Messages
32
hi

the property OpenFileDialog.FileName returns the whole path of the selected file. how do i get only the filename (without the path in front)?

best regards

georg
 
The only way I know (using C#) is to find the last slash and then get all characters after that.
[CS]
string strFile = OpenDialog1.FileName;
strFile = strFile.Substring(strFile.LastIndexOf("\"));
[/CS]
Using LastIndexOf("\") we find the last place the slash is used,
this denotes the last directory. Everything after that is the filename
and extension. So we use substring to obtain everything after that point.
 
You could also set the RestoreDirectory to false and then use the current directory and parse this out of your FileName, thus giving you the file name on its own.

It is a bit silly that there isint a property that has the name on its own.
 
Back
Top