Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get the DOS Path from windows full path?
For example, if I am in the directory "C:\Program Files\Java\jdk1.6.0_22", I want to display it's short name "C:\PROGRA~1\Java\JDK16~1.0_2".
Posted

Try using the GetShortPathName method[^].
However, this uses interop.

How to get the ShortPathName of a LongPathName in VB and VB.NET [^] is another example where unmanaged code does this for you.
 
Share this answer
 
Comments
Venkatesh Mookkan 29-Feb-12 6:32am    
Good one!
Abhinav S 29-Feb-12 6:38am    
Thank you. Off topic - you are not visiting the GIT anymore?
Venkatesh Mookkan 1-Mar-12 1:03am    
I am visiting daily. But not participating regularly due to project needs. I am glad to see more new peoples around!
For getting short path name you have to can un-managed API GetShortPathName

C#
public static string GetShortName(string sLongFileName) {
  var buffer = new StringBuilder(259);
  int len = GetShortPathName(sLongFileName, buffer, buffer.Capacity);
  if (len == 0) throw new System.ComponentModel.Win32Exception();
  return buffer.ToString();
}

[DllImport("kernel32", EntryPoint = "GetShortPathName", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetShortPathName(string longPath, StringBuilder shortPath, int bufSize);


Below provided links can provide you more help
http://stackoverflow.com/questions/2585171/getshortpathnamea-not-working-in-c-sharp[^]
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900