Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


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

GetLongPathName (kernel32)
 
.
Summary
Converts the specified path to its long form.

C# Signature:

using System.Runtime.InteropServices;
using System.Text;

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.U4)]
    private static extern int GetLongPathName(
        [MarshalAs(UnmanagedType.LPTStr)]
        string lpszShortPath,
        [MarshalAs(UnmanagedType.LPTStr)]
        StringBuilder lpszLongPath,
        [MarshalAs(UnmanagedType.U4)]
        int cchBuffer);

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

User-Defined Types:

None.

Notes:

The GetLongPathName API call is only available on Windows 98/ME and Windows 2000/XP. It is not available on Windows 95 & NT.

The file must exist to get the correct path. This is because the full path comes from the file system information. If an invalid name is given and exception is not thrown rather the name is just a blank space.

Tips & Tricks:

Please add some!

Sample Code:

/// <summary>
/// The ToLongPathName function retrieves the long path form of a specified short input path
/// </summary>
/// <param name="shortName">The short name path</param>
/// <returns>A long name path string</returns>
public static string ToLongPathName(string shortName)
{
    StringBuilder longNameBuffer = new StringBuilder(256);
    uint bufferSize = (uint)longNameBuffer.Capacity;

    /// <summary>
    /// Converts a short path to a long path.
    /// </summary>
    /// <param name="shortPath">A path that may contain short path elements (~1).</param>
    /// <returns>The long path.  Null or empty if the input is null or empty.</returns>
    internal static string GetLongPathName(string shortPath)
    {
    if (String.IsNullOrEmpty(shortPath))
    {
        return shortPath;
    }
    GetLongPathName(shortName, longNameBuffer, bufferSize);

    StringBuilder builder = new StringBuilder(255);
    int result = GetLongPathName(shortPath, builder, builder.Capacity);
    if (result > 0 && result < builder.Capacity)
    {
        return builder.ToString(0, result);
    }
    else
    {
        if (result > 0)
        {
        builder = new StringBuilder(result);
        result = GetLongPathName(shortPath, builder, builder.Capacity);
        return builder.ToString(0, result);
        }
        else
        {
        throw new FileNotFoundException(
            string.Format(
            CultureInfo.CurrentCulture,
            Resources.Strings.ProjectNotFound,
            shortPath),
            shortPath);
        }
    }
    }
    return longNameBuffer.ToString();
}

Alternative Managed API:

The short name can be used without problem to open files so the only time you would need to use GetLongPathName is if you want to display the path or want to display the file name.

With this alternative you cannot get the long name for the full path but it will resolve the name of the file.

/// <summary>
/// The ToLongName function retrieves the long file name form of a specified short input path
/// </summary>
/// <param name="shortName">The short name path</param>
/// <returns>A long file name string</returns>
public static string ToLongName(string shortName)
{
    string[] paths = Directory.GetFiles( Path.GetDirectoryName(shortName), Path.GetFileName(shortName) );

    string longName = "";
    if( paths.Length > 0 )
        longName = Path.GetFileNameWithoutExtension( paths[0] );

    return longName;
    return Path.GetFileNameWithoutExtension( paths[0] );
}

Documentation

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 PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions