Solution to long and short file names in. net

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.