[Original source code] (05): C #-calculate the absolute path.

Source: Internet
Author: User
[Original source code] (05): C #-calculate the absolute path.

Comments (vulnerabilities, performance, etc.) are welcome ). In blog Community Otherwise, please indicate the author and source. Thank you!  
/**/ ///   <Summary>
/// Calculate absolute path
///   </Summary>
///   <Param name = "absolutedir"> Absolute directory </Param>
///   <Param name = "relativefile"> Relative File </Param>
///   <Returns> </returns>
///   <Example>
/// @ "D: \ windows \ regedit.exe" = getabsolutepath (@ "D: \ windows \ WEB \ wallpaper \", @ "... \ regedit.exe ");
///   </Example>
Public   Static   String Getabsolutepath ( String Absolutedir, String Relativefile)
{
Bool Isnotover =   True ;
Int Intstart =   0 ;
While (Isnotover)
{
If (Relativefile. startswith ( @" ..\ " ))
{
Relativefile=Relativefile. Remove (0,3);
Intstart++;
}
Else
{
Isnotover= False;
}
}

If (Intstart >   0 )
{
If (Absolutedir. endswith ( " \\ " ))
{
Absolutedir=Absolutedir. Remove (absolutedir. Length- 1);
}

For ( Int I =   0 ; I < Intstart; I ++ )
{
Absolutedir=Absolutedir. Remove (absolutedir. lastindexof ("\\"));
}
}
Return Path. Combine (absolutedir, relativefile );
}
 

The efficiency of the following method is 1/50 upstairs...
Public static string getabsolutepath (string Dir, string path)
{
String strdir = directory. getcurrentdirectory ();
Directory. setcurrentdirectory (DIR );
String strnew = path. getfullpath (PATH );
Directory. setcurrentdirectory (strdir );
Return strnew;
}

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.