473,322 Members | 1,566 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,322 software developers and data experts.

Converting Long Directory Path's To Short Path's

How do you convert a path like this:

C:\Program Files

to

C:\PROGRA~1

In .NET? I have a program I have to call via a command line and calling one
of it's parameters I have to give it a file name and path. If that path has a
space in it and I wrap it with quotes, it does not seem to like it.

Thanks,
David


=====================
David McCarter
www.vsdntips.com
Jul 21 '05 #1
2 2941
Hello David.

You can use P/Invoke to call Win32 "GetShortPathName()" function to get the
short path:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern int GetShortPathName(string lpszLongPath,
System.Text.StringBuilder lpszShortPath, int cchBuffer);
static void Main(string[] args)
{
string sLongPath = "c:\\program files";
System.Text.StringBuilder sShortPath = new System.Text.StringBuilder(260);
GetShortPathName(sLongPath, sShortPath, 260);
}
Hope this helps.

Regards,
Igor Zinkovsky [MSFT]
Visual Studio Build/Release Team

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"dotNetDave" wrote:
How do you convert a path like this:

C:\Program Files

to

C:\PROGRA~1

In .NET? I have a program I have to call via a command line and calling one
of it's parameters I have to give it a file name and path. If that path has a
space in it and I wrap it with quotes, it does not seem to like it.

Thanks,
David


=====================
David McCarter
www.vsdntips.com

Jul 21 '05 #2
I was hoping for something managed, but this did the trick. Thanks!

David

"Igor Zinkovsky [MSFT]" wrote:
Hello David.

You can use P/Invoke to call Win32 "GetShortPathName()" function to get the
short path:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern int GetShortPathName(string lpszLongPath,
System.Text.StringBuilder lpszShortPath, int cchBuffer);
static void Main(string[] args)
{
string sLongPath = "c:\\program files";
System.Text.StringBuilder sShortPath = new System.Text.StringBuilder(260);
GetShortPathName(sLongPath, sShortPath, 260);
}
Hope this helps.

Regards,
Igor Zinkovsky [MSFT]
Visual Studio Build/Release Team

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"dotNetDave" wrote:
How do you convert a path like this:

C:\Program Files

to

C:\PROGRA~1

In .NET? I have a program I have to call via a command line and calling one
of it's parameters I have to give it a file name and path. If that path has a
space in it and I wrap it with quotes, it does not seem to like it.

Thanks,
David


=====================
David McCarter
www.vsdntips.com

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
0
by: Arjan van den Noort | last post by:
How to delete a directory (with all containing directories and files) with vb.net if the directory contains long paths ? (just like the dos command: RD /S/Q path) What I want is a routine to...
0
by: Rahul Chatterjee | last post by:
Hello All I have designed a dotnet application using VB which basically takes a selection and passes value to a crystal report which in turn passes the value to a Stored procedure. After the...
2
by: dotNetDave | last post by:
How do you convert a path like this: C:\Program Files to C:\PROGRA~1 In .NET? I have a program I have to call via a command line and calling one of it's parameters I have to give it a...
1
by: topramen | last post by:
does any one here know of a good way to to determine whether or not a given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir \myfile.txt")? i started attacking this problem by...
5
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
Before you respond with "just use GetShortPathName" please read the rest. We have an application that places files on a file server using mapped drives as it's path of choice. The reason for...
6
by: bg_ie | last post by:
Hi, My company's backend is located at a location with a long address, something like - //our_servers/server_number_one/our_department/our_devision/ our_results/our_databases/backend.be I...
1
by: David Buckley | last post by:
Does anyone have any example code of converting any type of video to flash video at all using asp.net 2.0 c# web applications. God Bless
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.