473,238 Members | 1,905 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,238 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 9815

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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.