I'm trying to get get the short filename from a long filename but I'm having problem in c# code. VB.Net code is:
Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Public Function GetShortName(ByVal sLongFileName As String) As String
Dim lRetVal As Long, sShortPathName As String, iLen As Integer
'Set up buffer area for API function call return
sShortPathName = Space(255)
iLen = Len(sShortPathName)
'Call the function
lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
'Strip away unwanted characters.
GetShortName = Left(sShortPathName, lRetVal)
End Function
I've converted this function to c#:
[DllImport("kernel32", EntryPoint = "GetShortPathNameA")]
static extern long GetShortPathName(string lpszLongPath, string lpszShortPath, long cchBuffer);
public string GetShortName(string sLongFileName)
{
long lRetVal;
string sShortPathName;
int iLen;
// Set up buffer area for API function call return
sShortPathName = new String(' ', 1024);
iLen = sShortPathName.Length;
// Call the function
lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen);
// Strip away unwanted characters.
return sShortPathName.Trim();
}
But I cant get the c# version work. Am I missing something or what is wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…