c#

c#相对路径获取的方法是什么

小亿
86
2024-03-04 18:53:07
栏目: 编程语言

在C#中获取相对路径的方法通常使用Path类的Combine方法。该方法可以将基础路径和相对路径组合在一起,得到完整的路径。示例如下:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = @"C:\Users\Public";
        string relativePath = @"Documents\MyFile.txt";

        string fullPath = Path.Combine(basePath, relativePath);

        Console.WriteLine(fullPath);
    }
}

在上面的示例中,basePath是基础路径,relativePath是相对路径,使用Path.Combine方法将它们组合在一起得到完整的路径。

0
看了该问题的人还看了