Convert non existant path to Dos 8.3

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
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Thank you for posting in the community!

Based on my understanding, when you use GetShortPathName() to conver the
file path, you meet a problem, if the file path existed, it works well. But
if the file does not exist, it will fail.

========================================
Actually, It is not the C# code issue. This is the default behavior of
GetShortPathName Win32 API function.

This function will search for the long path file first. You can determine
this through invoke GetShortPathName function in C++ code like this:

#include "stdafx.h"
#include <windows.h>
int main(int argc, char* argv[])
{
char buf[80];
DWORD ret = GetShortPathName("D:\\Temp\\YourLongAppName.exe", buf,
80);
int err=GetLastError();
return 0;
}

If the YourLongAppName does not exist, the err will be 002, which means
"The system cannot find the file specified. "(You can find its related
message in "Error Lookup" tool)

It is the same behavior as P/invoke in C#.

======================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Michael Sparks

"Jeffrey Tan[MSFT]" said:
Actually, It is not the C# code issue. This is the default behavior of
GetShortPathName Win32 API function.

For some additional insight, I believe the reason deals with the long
filename extensions for the FAT file system. Suppose I have two folders:

c:\somelongpath_dog\
c:\somelongpath_cat\

Whichever path I create *first* will have the short name "c:\somelo~1\".
The second path I create will have the short name "c:\somelo~2\".

In your case, since the folder doesn't exist yet, the system can't guarantee
what ~n it would end up getting.

HTH
Mike
 
G

Guest

Hi Jim,

Does our reply make sense to you?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Mitch

I just came across your postings when I was trying to do the same
thing with paths that don't exist. I was going to write a whole
function that simulates GetDosPath but instead I just created a
temporary file, found the short dos name, then deleted the file. It
works fine in my app, but then again I'm only dealing with a handful
of files at a time, so I have not needed to worry about performance
issues.
Hope this is helpful to some.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top