无法获取文件 C# 的绝对路径

Can't get absolute path to file C#

我的程序很简单。这里我设置了绝对路径,但是C#认为它是一个相对路径并尝试从项目目录加载文件:C:\Users\Gleb Kozyukevich\source\repos\ChangeDir\ChageDir\bin\Debug\netcoreapp3.1\C:\test\test.txt

路径确实存在。

我错过了什么?听不懂

您可以尝试多种方法。

  1. 文件路径如
string sourceFilePath  = @"C:\test\test.txt";
  1. 使用System.IO.Path.Combine
string sourceFilePath = System.IO.Path.Combine(new []{"C:","test","test.txt"});

这很奇怪。我已经按照您认为的方式重现了代码,结果很好。

这里是我写的代码:

using System;
using System.IO;

namespace ReadAllLines
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"c:\temp\test.txt");
            
            Console.ReadKey();
        }
    }
}

结果如下: