Converting Long Directory Path's To Short Path's

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
I was hoping for something managed, but this did the trick. Thanks!

David

Igor Zinkovsky said:
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 said:
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
 
Back
Top