When I use the path. gettempfilename () function to retrieve a directory name, the following result is displayed: C: \ e ~ 1 \ vitami ~ 1... It should be c: \ Documents and Settings \ vitaminc.net. After searching for half a day, I finally found the result in gotdotnet Code Paste it out and check it out:
Using System;
Using System. text;
Using System. runtime. interopservices;
Namespace Shellpathnameconvert
{
/**/ /// <Summary>
/// Converts file and directory paths to their respective
/// Long and Short Name versions.
/// </Summary>
/// <Remarks> This class uses interopservices to call getlongpathname and getdomainpathname </Remarks>
Public Class Convert
{
[Dllimport ( " Kernel32.dll " )]
Static Extern Uint Getlongpathname ( String Shortname, stringbuilder
Longnamebuff, Uint Buffersize );
[Dllimport ( " Kernel32.dll " , Charset = Charset. Auto)]
Public Static Extern Int Getmediapathname (
[Financialas (unmanagedtype. lptstr)]
String Path,
[Financialas (unmanagedtype. lptstr)]
Stringbuilder export path,
Int Required pathlength );
/**/ /// <Summary>
/// The tow.pathnametolongpathname function retrieves the long path form of a specified short input path
/// </Summary>
/// <Param name = "shortname"> The short name path </Param>
/// <Returns> A long name path string </Returns>
Public Static String Tolongpathname ( String Shortname)
{
Stringbuilder longnamebuffer = New Stringbuilder ( 256 );
Uint Buffersize = ( Uint ) Longnamebuffer. capacity;
Getlongpathname (shortname, longnamebuffer, buffersize );
Return Longnamebuffer. tostring ();
}
/**/ /// <Summary>
/// The tolongpathnametow.pathname function retrieves the short path form of a specified long input path
/// </Summary>
/// <Param name = "longname"> The long name path </Param>
/// <Returns> A short name path string </Returns>
Public Static String Tow.pathname ( String Longname)
{
Stringbuilder shortnamebuffer = New Stringbuilder ( 256 );
Int Buffersize = Shortnamebuffer. capacity;
Int Result = Getmediapathname (longname, shortnamebuffer, buffersize );
Return Shortnamebuffer. tostring ();
}
}
}
Running result:
Current directory:
D: \ Documents and Settings \ Administrator \ Desktop \ shellpathnameconvert \ shellpathnamec
Onverttest \ bin \ debug
Short Path Name:
D: \ docume ~ 1 \ admini ~ 1 \ Desktop \ shellp ~ 1 \ shellp ~ 2 \ bin \ debug
Long Path Name:
D: \ Documents and Settings \ Administrator \ Desktop \ shellpathnameconvert \ shellpathnamec
Onverttest \ bin \ debug
With the author's note:
Shellpathnameconvert will allow you to convert to and from long and short paths. it's common for the Windows Shell to send you a short path if your application takes command line arguments. however, it's not always convenient to work with the short path names and. NET provides no built in way to get the long path Nam