Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone (and Happy New Year to all)

I am using a basic piece of code to select a file but I am only able to get the full path (which I need) but can't seem to find a way to extract the file name and extension only
VB
Dim ofd As New OpenFileDialog
Dim dir As String

dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

ofd.Filter = "All Files(*.*)|*.*|All files (*.*)|*.*"

ofd.FilterIndex = 2
ofd.InitialDirectory = dir

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
     MsgBox(ofd.FileName)
End If


The MsgBox shows the full path with file name and extension.

Could you please advise how I can get only the file name and extension.

Many thanks
Posted

Try:
VB
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
     MsgBox(Path.GetFileName(ofd.FileName))
End If
 
Share this answer
 
Comments
Darrell de Wet 6-Jan-14 9:21am    
Thank you.
OriginalGriff 6-Jan-14 9:30am    
You're welcome!
Please visit the following links:

Get file name from a path string in c-sharp[^]
Get the file name[^]

VB
Dim ofd As New OpenFileDialog
  Dim dir As String

  dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

  ofd.Filter = "All Files(*.*)|*.*|All files (*.*)|*.*"

  ofd.FilterIndex = 2
  ofd.InitialDirectory = dir

  If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
       MsgBox(Path.GetFileName(ofd.FileName))
  End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900