Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

-Sean
Posted

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 isin't a property that has the name on its own.

Hamlet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...