How to get the ShortPathName of a LongPathName in VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
 

There could be a time when you may need and/or want to convert a path to its ShortPath name alternative. Some APIs may even recommend you 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 Microsoft API call function. The function is called: GetShortPathName() and as the name suggests, will convert the path in the parameter to it short Path equivalent.   To get started you will want to put the API Function in your project. I usually just place it near the top of the code in the declaration section.

_____________________________________________________

Visual Basic 5.0/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 2002/2003 and Visual Basic 2005/2008/2010 Version…

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. Example is below for Visual Basic Classic (5.0/6.)…

'
'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)

 

Now for the Visual Basic.NET 2002/2003/2005/2008/2010 Code Version…

 

'
'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)  

 
_____________________________________________________
 

Thats all there is to it! The new ShortPathName from the Long path in the example code above returns the path as: “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

One thought on “How to get the ShortPathName of a LongPathName in VB and VB.NET

  1. Lam

    Hi

    I tried the above for VB.net 2003
    It works,
    but for long path names less then 8+3 char
    it does not seem to work

    works for
    c:\This is long path\Another long file.txt
    returns
    C:\THISIS~1\MYFILE~1.TXT

    does not work for
    c:\This is\My File.txt
    should return as
    c:\THISIS~1\MYFILE~1.txt
    but returns Null ?

    Reply

Leave a Reply