long path to short path
-
HI, My program has to trigger a dumb application that won't take paths with spaces.:rolleyes: Do you know if there is a built in function in C# that translates long file paths to short file paths. Thank you, Elena
-
HI, My program has to trigger a dumb application that won't take paths with spaces.:rolleyes: Do you know if there is a built in function in C# that translates long file paths to short file paths. Thank you, Elena
There is nothing in the .NET base class library that does this (at least nothing public that I can remember / find), but - if your app runs on Windows 2000 or higher (any NT-based system) - you can P/Invoke the function
PathGetShortPath
:[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
public static extern void PathGetShortPath(string path);You pass the long path into the method and after it returns
path
contains the short path. If you need something that works on all operating systems that .NET runs on, you'll have to enumerate each directory and the filename and change it to the 8.3 filename format. It's been a long time since I've worked with these, so you might want to check the PSDK to see if it documents how similarily named files or directories are resolved using the "~N" notation (like "MICROS~1", "MICROS~2", etc.). You should also trying googling for the solution. I'm sure someone else has had to deal with this before.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
There is nothing in the .NET base class library that does this (at least nothing public that I can remember / find), but - if your app runs on Windows 2000 or higher (any NT-based system) - you can P/Invoke the function
PathGetShortPath
:[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
public static extern void PathGetShortPath(string path);You pass the long path into the method and after it returns
path
contains the short path. If you need something that works on all operating systems that .NET runs on, you'll have to enumerate each directory and the filename and change it to the 8.3 filename format. It's been a long time since I've worked with these, so you might want to check the PSDK to see if it documents how similarily named files or directories are resolved using the "~N" notation (like "MICROS~1", "MICROS~2", etc.). You should also trying googling for the solution. I'm sure someone else has had to deal with this before.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Does not work on NT. It's only for 2000 and higher. This API function call works on both OSes : ["kernel32.dll"] GetShortPathName(string szLongPath, string szShortPath, long dwLength).