G
Guest
Hi all,
I'm trying to convert a string representing a path to Dos 8.3 notation. The code that I have works fine if the path exists, but it will not work if the path does not exist. I am looking for a way to convert a string representing a long file path (whether or not it exists) to short (dos 8.3) form. This is what I have:
private string GetShortPath(string longPath) {
if (longPath.Length <= 8) {
return longPath;
}
StringBuilder shortPath = new StringBuilder(255);
int result = GetShortPathName(longPath, shortPath, shortPath.Capacity);
Uri shortPathUri = null;
try {
shortPathUri = new Uri("file://" + shortPath.ToString());
}
catch (Exception) {
throw new DirectoryNotFoundException("Directory " + longPath + " not found.");
}
string[] shortPathSegments = shortPathUri.Segments;
if (shortPathSegments.Length == 0) {
return longPath;
}
if (shortPathSegments.Length == 1) {
return shortPathSegments[0];
}
return shortPathSegments[shortPathSegments.Length-1];
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern int GetShortPathName(string LongPath, StringBuilder ShortPath, int BufferSize);
Thanks for any help with this
Jim
I'm trying to convert a string representing a path to Dos 8.3 notation. The code that I have works fine if the path exists, but it will not work if the path does not exist. I am looking for a way to convert a string representing a long file path (whether or not it exists) to short (dos 8.3) form. This is what I have:
private string GetShortPath(string longPath) {
if (longPath.Length <= 8) {
return longPath;
}
StringBuilder shortPath = new StringBuilder(255);
int result = GetShortPathName(longPath, shortPath, shortPath.Capacity);
Uri shortPathUri = null;
try {
shortPathUri = new Uri("file://" + shortPath.ToString());
}
catch (Exception) {
throw new DirectoryNotFoundException("Directory " + longPath + " not found.");
}
string[] shortPathSegments = shortPathUri.Segments;
if (shortPathSegments.Length == 0) {
return longPath;
}
if (shortPathSegments.Length == 1) {
return shortPathSegments[0];
}
return shortPathSegments[shortPathSegments.Length-1];
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern int GetShortPathName(string LongPath, StringBuilder ShortPath, int BufferSize);
Thanks for any help with this
Jim