c#相对路径使用的方法是什么

相对路径是相对于当前工作目录或当前文件所在目录的路径,可以使用以下方法来使用相对路径:

使用相对路径时,需要先获取当前目录的路径,可以使用Directory.GetCurrentDirectory()方法获取当前工作目录的路径。

string currentDirectory = Directory.GetCurrentDirectory();

使用相对路径时,可以使用Path.Combine()方法将相对路径和当前目录路径组合成完整的路径。

string relativePath = "folder/file.txt";
string fullPath = Path.Combine(currentDirectory, relativePath);

使用完整路径时,可以直接使用File.ReadAllText()等方法读取文件内容。

string content = File.ReadAllText(fullPath);

通过以上方法,就可以使用相对路径来访问文件或目录。

阅读剩余
THE END