locked
get only fileName (not full path) RRS feed

  • Question

  • User-1210839387 posted

    using asp.net/vb.net 2005.

    Is there an easy way other than splitting the string of getting only the filename (not the full path, the short name) of a file, what I have now is:


    Dim fileEntries As String() = Directory.GetFiles(myDirectory)
    Dim fileName As String
    Dim longFileName As String
    Dim shortFileName As String
    
    For Each fileName In fileEntries
         'gives me the long file name, ex: //myServer/myfolder/fileName.jpg
         longFileName = fileName
    
        ' I need to get only the short file name here, ex: fileName.jpg, how to do this?
    Next


    Monday, January 10, 2011 8:06 AM

Answers

  • User-443671128 posted

    Hi,

    You can retrieve only filename from full path by using ...

    Path.GetFileName("Your File Full Path")


    and if you want to get only filename without extension than use ...

    Path.GetFileNameWithoutExtension





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 10, 2011 8:12 AM

All replies

  • User-1998067147 posted

    Hi,

    Try Path.GetFileName(fileName) it will return you only the fileName.

    Regards,

    Ahsan Muhammad Khan

    Monday, January 10, 2011 8:11 AM
  • User-443671128 posted

    Hi,

    You can retrieve only filename from full path by using ...

    Path.GetFileName("Your File Full Path")


    and if you want to get only filename without extension than use ...

    Path.GetFileNameWithoutExtension





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 10, 2011 8:12 AM