Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetShortPathName (kernel32)

C# Signature:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern uint GetShortPathName(
   string lpszLongPath,
   StringBuilder lpszShortPath,
   uint cchBuffer);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(string lpszLongPath, char[] lpszShortPath, int cchBuffer);

// To ensure that paths are not limited to MAX_PATH, use this signature within .NET
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)]
static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int cbShortName);

VB.NET Signature:

Imports System.Runtime.InteropServices

'/// KSG Comments: This code has only been tested for .NET 2.x
   Public Class Interop
      <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
      Public Shared Function GetShortPathName(ByVal longPath As String, _
          <MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, _
          <MarshalAs(Runtime.InteropServices.UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer
      End Function
   End Class
   Declare Unicode Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameW" (ByVal longPath As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, <MarshalAs(UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer

C++/CLI Signature:

  [DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError=true)]
static int GetShortPathName(
    String^ lpszLongPath,
    System::Text::StringBuilder^ lpszShortPath,
    unsigned int cchBuffer);

User-Defined Types:



Important! The file must actually exist, otherwise you'll get an empty path

Tips & Tricks:

Please add some!

Sample Code:

/// <summary>
/// The ToLongPathNameToShortPathName function retrieves the short path form of a specified long input path
/// </summary>
/// <param name="longName">The long name path</param>
/// <returns>A short name path string</returns>
public static string ToShortPathName(string longName)
    uint bufferSize = 256;

    // donĀ“t allocate stringbuilder here but outside of the function for fast access
    StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);

    GetShortPathName(longName, shortNameBuffer, bufferSize);

    return shortNameBuffer.ToString();

public string ShortPath(string longpath)
    char[] buffer = new char[256];

    GetShortPathName(longpath, buffer, buffer.Length);

    return new string(buffer);

// Memory friendly version with error checking
public static string GetShortPathName(string longFileName)
    int sz = GetShortPathName(longFileName, null, 0);
    if (sz == 0)
       throw new Win32Exception();
    var sb = new StringBuilder(sz + 1);
    sz = GetShortPathName(longFileName, sb, sb.Capacity);
    if (sz == 0)
       throw new Win32Exception();
    return sb.ToString();

VB.NET Sample Code:

/// KSG Comments:
/// Unlike the sample above I am check for the return code in the event of an error
/// KSG Changed declaration for .NET 2.x 01/25/2007
   Public Class Interop
      <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
      Public Shared Function GetShortPathName(ByVal longPath As String, _
      <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.LPTStr)> _
      ByVal ShortPath As System.Text.StringBuilder, _
      <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.U4)> _
      ByVal bufferSize As Integer) As Integer
      End Function
   End Class

   Function ShortPathName(ByVal Path As String) As String
      Dim sb As New System.Text.StringBuilder(1024)

      Dim tempVal As Integer = Interop.GetShortPathName(Path, sb, 1024)
      If tempVal <> 0 Then
     Dim Result As String = sb.ToString()
     Return Result
     Throw New Exception("Failed to return a short path")
      End If
   End Function

Alternative Managed API:

Do you know one? Please contribute it!


Neither of these works in VB2005 Express.

Ok, I have been playing with this a bit more & found a solution...this is VB2005 Express.

I was trying to put the "GetShortName" into a module and calling it from my Form (as I am used to doing). What I found, and it works for some reason, was to put this in my module:

VB.NET Signature:

Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer

...And I had to put this actually in the form in my event (Me.ComboBox1.DblClick):

VB.NET Sample Code:

Dim RetVal As Integer

Dim ShortFileName As String

Dim GetShortFileName As String

ShortFileName = Microsoft.VisualBasic.Space(260)

RetVal = GetShortPathName(FindMedia, ShortFileName, Microsoft.VisualBasic.Len(ShortFileName))

GetShortFileName = Microsoft.VisualBasic.Left(ShortFileName, RetVal)


Spaghetti code, yes, but at least it works


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version