[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;
}