
我有非常簡單的程式。這里我設定了絕對路徑,但是 C# 認為它是相對路徑并嘗試從專案目錄加載檔案:C:\Users\Gleb Kozyukevich\source\repos\ChangeDir\ChageDir\bin\Debug\netcoreapp3.1\C:\test\test.txt
路徑確實存在。
我錯過了什么?我無法理解
uj5u.com熱心網友回復:
您可以嘗試多種方法。
- 給出檔案路徑,如
string sourceFilePath = @"C:\test\test.txt";
- 使用 System.IO.Path.Combine
string sourceFilePath = System.IO.Path.Combine(new []{"C:","test","test.txt"});
uj5u.com熱心網友回復:
這很奇怪。我已經按照我認為您撰寫的方式復制了代碼,結果很好。
這里是我寫的代碼:
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();
}
}
}
結果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442256.html
