How to Get the ShortPathName from a LongPathName in Visual Basic and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

There could be a time when you may need to convert a path to its short name alternative. Some APIs may need you to use the Short Path representation of the normal long path name. To convert a long path to a short path name is very easy to do thanks to a little function. The function is called: GetShortPathName() and as the name suggests, it will convert the path to its short path equivalent. To get started you will want to put the API Function in declaration section of your project.

Visual Basic 6.0 Version

    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal longPath As _
        String, ByVal shortPath As String, ByVal shortBufferSize As Long) As Long

Visual Basic.NET, VB 2008, Visual Basic 2010, and VB 2013

    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal longPath As _
        String, ByVal shortPath As String, ByVal shortBufferSize As Int32) As Int32

And now you just need to call that function with your data in the parameters.

Visual Basic 6.0

        'The path you want to convert to its short representation path.
        Dim longPathName As String

        longPathName = "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705"

        'Get the size of the string to pass to the string buffer.
        Dim longPathLength As Long

        longPathLength = Len(longPathName)

        'A string with a buffer to receive the short path from the api call…
        Dim shortPathName As String

        shortPathName = Space$(longPathLength)

        'Will hold the return value of the api call which should be the length.
        Dim returnValue As Long

        'Now call the function to do the conversion…
        returnValue = GetShortPathName(longPathName, shortPathName, longPathLength)

        MsgBox(shortPathName)

Visual Basic.NET, VB 2008, Visual Basic 2010, and VB 2013

        'The path you want to convert to its short representation path.
        Dim longPathName As String = "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705"

        'Get the size of the string to pass to the string buffer.
        Dim longPathLength As Int32 = longPathName.Length

        'A string with a buffer to receive the short path from the api call…
        Dim shortPathName As String = Space(longPathLength)

        'Will hold the return value of the api call which should be the length.
        Dim returnValue As Int32

        'Now call the function to do the conversion…

        returnValue = GetShortPathName(longPathName, shortPathName, longPathLength)

        MsgBox(shortPathName.ToString)

That’s all there is to it! The new ShortPathName from the Long path in the example code above converts the path to: C:\WINDOWS\MICROS~1.NET\FRAMEW~1\V10~1.370

So if you’ve been needing a way to get a short path of a long path, this code should do it for you just fine.

Jason

Leave a Reply