扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-05-14 18:54:11
c#

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

  1. 使用相对路径时,需要先获取当前目录的路径,可以使用Directory.GetCurrentDirectory()方法获取当前工作目录的路径。
string currentDirectory = Directory.GetCurrentDirectory();
  1. 使用相对路径时,可以使用Path.Combine()方法将相对路径和当前目录路径组合成完整的路径。
string relativePath = "folder/file.txt";
string fullPath = Path.Combine(currentDirectory, relativePath);
  1. 使用完整路径时,可以直接使用File.ReadAllText()等方法读取文件内容。
string content = File.ReadAllText(fullPath);

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

扫码添加客服微信