[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
/// KSG Comments: This code has only been tested for .NET 2.x
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
[DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError=true)]
static int GetShortPathName(
[MarshalAs(UnmanagedType::LPTStr)]
String^ lpszLongPath,
[MarshalAs(UnmanagedType::LPTStr)]
System::Text::StringBuilder^ lpszShortPath,
unsigned int cchBuffer);
None.
None.
Please add some!
/// <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)
{
StringBuilder shortNameBuffer = new StringBuilder(256);
int bufferSize = shortNameBuffer.Capacity;
int result = GetShortPathName(longName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
/// 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
Else
Throw New Exception("Failed to return a short path")
End If
End Function
Do you know one? Please contribute it!