By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,894 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,894 IT Pros & Developers. It's quick & easy.

Convert non existant path to Dos 8.3

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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.

Nov 15 '05 #2

P: n/a
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Qe**************@cpmsftngxa07.phx.gbl...
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
Nov 15 '05 #3

P: n/a

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.

Nov 15 '05 #4

P: n/a
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.
v-*****@online.microsoft.com (""Jeffrey Tan[MSFT]"") wrote in message news:<CC**************@cpmsftngxa07.phx.gbl>...
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.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.