Search code examples
vb.netopenfiledialog

get a file name in openfiledialog


I use OpenFileDialog in VB.NET for importing file from C:\ , but when I choose my file , can I show ONLY the file name (not a complete path ) ? enter image description here

I want to see only namefile.pdf, and not c:....\namefile.pdf

Here is my code:

fichier.Filter = "PDF Files|*.pdf|image files|*.png;*.jpg;*.gif"
If fichier.ShowDialog = DialogResult.OK Then
   fichier_txt.Text = fichier.FileName
End If

fichier is OpenFileDialog name


Solution

  • Use Path.GetFileName.

    fichier.Filter = "PDF Files|*.pdf|image files|*.png;*.jpg;*.gif"
    If fichier.ShowDialog = DialogResult.OK Then
       fichier_txt.Text = Path.GetFileName(fichier.FileName)
    End If