'Can't get absolute path to file C#
I have very simple program. Here I set the absolute path,but C# thinks that it's a relative path and try to load the file from project directory: C:\Users\Gleb Kozyukevich\source\repos\ChangeDir\ChageDir\bin\Debug\netcoreapp3.1\C:\test\test.txt
The path really exists.
What did I miss? I can't get understand
Solution 1:[1]
There are multiple ways you try.
- Give file path like
string sourceFilePath = @"C:\test\test.txt";
- Use System.IO.Path.Combine
string sourceFilePath = System.IO.Path.Combine(new []{"C:","test","test.txt"});
Solution 2:[2]
That is very strange. I've reproduced the code the way I think you wrote it and the result is just fine.
Here the code I wrote:
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();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dotnetstep |
| Solution 2 | Paul Sinnema |


