Search code examples
c#fileiopathfile-read

File.ReadAllLines prepends executable path to provided path


I've come across a very strange error. So strange even that it seems like a bug to me, but what do I know.

I use the File.ReadAllLines(path) method in my code, like so:

  string[] lines = File.ReadAllLines(path);

So far so simple. The path I read is on my C drive, so its in the following format: C:\Folder\Folder\File.txt

Putting a break-point at the above line and hovering over the path variable shows the correct path

Here is where it gets strange. If I actually run this code I get an IOException, which informs me of an invalid path. The path it shows seems to be the path of my applications executable location + the path I passed to the method.

'C:\Users\kingv\Projects\C#\AUDS\Bsp2\bin\Debug\netcoreapp3.1\‪C:\Users\kingv\Projects\C#\AUDS\data\asc1k.txt

This error also occurs when using the File.ReadAllText method.

Whats weirder is that this seems to be a project specific issue. I have two projects in this solution. Except for very few differences, they have the exact same code, and the code relevant to my issue is exactly the same (The declaration and reading of the path)

I have never had problems with the File.ReadAllLines method before. Anybody got a clue what is going on here?


Solution

  • Reproduced your problem, but only was able to do so by copying the path written by the error message in your question.

    Your path has an illegal character at the beginning, therefore it is not considered as an absolute, rooted path, instead, considered as a relative path.

    You need to revise the source of the path, or if it is a literal just delete "C:\ part and write it again.

    You can see this by:

    Encoding.UTF8.GetString(path)
    

    As far as I can see, it is Unicode #8234 which is something like this:

    enter image description here

    This can also be seen when checking the element content with developer tools of chrome:

    enter image description here