1

I am trying to read txt file in C# but having error that

The given's path not supported

I have done this a lots of time in application. But I don't know what is the issue. Here is my code:

 var filePath = @"‪E:\P1.txt";

 string[] lines = System.IO.File.ReadAllLines(filePath);

It throws exception that path not supported. What's the issue?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62
  • 1
    If I copy paste your code it gives me the same error, if I write myself exactly the same string it works. Weird. Try rewrite your filePath string? – ALFA Mar 15 '19 at 11:43
  • 2
    Check the length of the file name - by the look of it it should be 9 - filename.Length = 10 - so there is a non-displaying character in there. – PaulF Mar 15 '19 at 11:45

2 Answers2

7

Let's have a look (print out the string dump):

var filePath = @"‪E:\P1.txt";

Console.Write(string.Join(Environment.NewLine, 
                          filePath.Select(c => $"'{c}' : 0x{(int)c:x4}")));

Outcome:

'‪' : 0x202a    <- LEFT-TO-RIGHT EMBEDDING
'E' : 0x0045
':' : 0x003a
'\' : 0x005c
'P' : 0x0050
'1' : 0x0031
'.' : 0x002e
't' : 0x0074
'x' : 0x0078
't' : 0x0074

Can you see invisible 0x202a (LEFT-TO-RIGHT EMBEDDING) symbol at the very start of the string?

https://www.fileformat.info/info/unicode/char/202a/index.htm

this symbol makes the path being invalid.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
6

As shown below, your file path contains the non-displayable Left-to-Right Embedding (LRE) Unicode character.

"\u202AE:\P1.txt"

If you just delete the line and write the path again, your problem will most likely be resolved.

prd
  • 2,272
  • 2
  • 17
  • 32