在C#中获取相对路径的方法通常使用Path类的Combine方法
在C#中获取相对路径的方法通常使用Path类的Combine方法。该方法可以将基础路径和相对路径组合在一起,得到完整的路径。示例如下:
usingSystem;
usingSystem.IO;
classProgram
{
staticvoidMain()
{
stringbasePath=@"C:\Users\Public";
stringrelativePath=@"Documents\MyFile.txt";
stringfullPath=Path.Combine(basePath,relativePath);
Console.WriteLine(fullPath);
}
}
在上面的示例中,basePath是基础路径,relativePath是相对路径,使用Path.Combine方法将它们组合在一起得到完整的路径。
版权声明
本文仅代表作者观点,不代表博信信息网立场。