Click to See Complete Forum and Search --> : OpenFileDialog: Save file name, but don't include path?


Ciralia
April 24th, 2006, 03:51 PM
With an OpenFileDialog object, you can say:


Dim openFileDialog As New OpenFileDialog()
Dim fileName As String

fileName = openFileDialog.FileName()


This code gets the file name as a string but it also includes the path like: C:\Desktop\temp.txt. Is there a way to get the file name itself without the path, like temp.txt?

kebo
April 24th, 2006, 05:37 PM
not directly but its not hard using the substring method

Dim fbrs As New OpenFileDialog
fbrs.ShowDialog()

Dim f As String = fbrs.FileName
Dim s As String = f.Substring(f.LastIndexOf("\"c) + 1)
MessageBox.Show(s)
HTH
kevin

jhammer
April 25th, 2006, 02:53 AM
You can use the FileInfo class:

Dim fbrs As New OpenFileDialog
fbrs.ShowDialog()

Dim f As New FileInfo(fbrs.FileName)

Dim fileName As String

fileName = f.Name