Saturday, March 10, 2007

DOS FileName To Windows FileName

Here is the code to convert filename in DOS format (8.3) to long filename.

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetLongPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder longPath,
int longPathLength
);


StringBuilder sb = new StringBuilder(255);
GetLongPathName(@"c:\BIGGGF~1\BIGGGF~1.txt",sb,255);


To do the reverse , use the following API


[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);


I wonder if there is a .Net way of doing same rather than depending on the API's