473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert non existant path to Dos 8.3

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
4 9834

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
""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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Scott Tilton | last post by:
We have a field that gets filled into a database that our ASP website pulls from. What we would like to be able to do is to take that path and convert it to a virtual path so the website can...
11
by: Altramagnus | last post by:
I have a complicated Region object, which I need to draw the outline, but not fill How can I convert the Region object to GraphicsPath object? or How can I draw the outline of the Region object?
4
by: | last post by:
I have earlier used an HttpModule that did URL rewrites on the BeginRequest event. Now I am trying to use the same module in a different application on a new and upgraded machine (winxp sp2). ...
2
by: Martin | last post by:
I would like to convert this code to VB.NET. Can someone help. Thanks. Dim X() Dim i As Long Dim objShell, objFolder, objFolderItem Dim FSO, oFolder, Fil Sub MainExtractData()
2
by: Anders K. Olsen | last post by:
Hello Group I have a fileserver with several files. Some of the filenames contain danish characters (e.g. 'æ', 'ø', 'å'). Now I'm trying to make a web application in C# that presents links to...
13
by: kbperry | last post by:
Hi all, Background: I need some help. I am trying to streamline a process for one of our technical writers. He is using Perforce (version control system), and is constantly changing his word...
10
by: Andrew Backer | last post by:
I have a few file:///c:/windows/somewhere style uris that I need to convert to the actual physical path. I am hoping there is a built in way to handle this. I know I can do some text replacing...
6
by: Ty | last post by:
Here is another block that I can not get to convert. This is a class module. public bool IsAuthenticated(string domain, string username, string pwd) { string domainAndUsername = domain + @"\"...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.