can anyone help me with how to retrieve only the filename and extension with openfiledialog.
when I use openfiledialog.filename I get c:\folder\filename ( just like the documentation says) and cant seem to find an easy way to get just the filename.
I am using visual basic express.
thanks

obtain filename only from opendialog
GregV
hi,
you can try something like that
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyOpenDialog As New OpenFileDialog
MyOpenDialog.Filter = "text files |*.txt|Html Files|*.html;*.htm" MyOpenDialog.Title = "Import Database" Dim result As DialogResult = MyOpenDialog.ShowDialog()
If Not result = Windows.Forms.DialogResult.Cancel Then Dim path As String = MyOpenDialog.FileName
MessageBox.Show(path)
'if you want to get ride of extention Dim indx As Integer = path.IndexOf(".")
Dim filepath As String = path.Substring(0, indx)
MessageBox.Show(filepath)
'just the file name indx = path.LastIndexOf("\")
Dim filename As String = path.Substring(indx + 1)
MessageBox.Show(filename)
End If End Sub
hope this helps
sharon n.
ra7207
try this
If CommonDialog1.FileName <> "" Then
iPos = InStrRev(CommonDialog1.FileName, "\")
txtDocName.Text = Mid(CommonDialog1.FileName, iPos + 1)
End If
jherbst
TheSmithz
Hi,
You can also use System.IO.Path class which provides many shared methods for manipulating file path strings in common scenarios.
For example if you want to retrieve only the file name part of an absolute path, you can use:
System.IO.Path.GetFileName(path)
Where path is the string variable for the absolute path, it also contains methods for getting the file extension, the directory path etc.
Hope this helps,