-1

i try to read Text file line by line:

static void Main(string[] args)
{
    int counter = 0;
    string line;
    string links = @"‪D:\links.txt";

    // Read the file and display it line by line.
    System.IO.StreamReader file = new System.IO.StreamReader(links);
    while ((line = file.ReadLine()) != null)
    {
        Console.WriteLine(line);
        counter++;
    }

    file.Close();

    // Suspend the screen.
    Console.ReadLine();
}

And got an error:

An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

Additional information: The given path's format is not supported.

I am working with Windows 10 Any suggestions what could cause this error ? (the file exist in this path)

Dana Yeger
  • 617
  • 3
  • 9
  • 26

1 Answers1

2

The issue is that there is an invisible character in your string. I don't know how they get there, others have guessed that the source code went through a program such as Word which modifies the text unbeknownst to you. Or you copied and pasted it from somewhere and that somewhere has the invisible character in it.

As for the character, I found it by first copying your string (if I just type out your string verbatim then it won't have the invisible character, it has to be copied from your text), then deleting everything between the quotes, then using a bitconverter to expose the actual bytes that your IDE displays as an invisible character:

Console.WriteLine(BitConverter.ToString(Encoding.UTF8.GetBytes(@"‪")));

gives:

E2-80-AA

Which is "Unicode Character 'LEFT-TO-RIGHT EMBEDDING' (U+202A)". Note that @"‪".Length returns 1 here because of the 1 invisible character.

This string, which I typed out by hand instead of copying from your string, doesn't have the invisible character: Console.WriteLine(BitConverter.ToString(Encoding.UTF8.GetBytes(@""))); and just returns a string.Empty with a length of 0.

A solution is to erase that entire string (including the "'s) and just type it out yourself, or highlight only the visible characters between the quotes and copy/paste them into a new string. Here is a fixed string for you: @"D:\links.txt", you can copy and paste that in and your problem will be fixed.

Community
  • 1
  • 1
Quantic
  • 1,779
  • 19
  • 30
  • How can i do that ? – Dana Yeger Sep 01 '16 at 18:29
  • @DanaYeger see my edit for some options on how to fix the string. – Quantic Sep 01 '16 at 18:32
  • I need to put this line ? Console.WriteLine(BitConverter.ToString(Encoding.UTF8.GetBytes(@"‪"))); ?? – Dana Yeger Sep 02 '16 at 03:41
  • @DanaYeger No, I was explaining what went wrong in that part... You need to open the code, highight all the text on the `string links = @"D:\links.txt";` line, then hit "Backspace" on your keyboard to erase that text, then using your keyboard type this in: `string links = @"D:\links.txt";`. That will fix your problem. – Quantic Sep 02 '16 at 14:55
  • Another way to check for unseen characters is to open your code in a hex editor. In Visual Studio (2019), right-click the file in `Solution Explorer`, select `Open With...`, and then select `Binary Editor`. For files outside the opened solution, open `File` → `Open`, select the file, click the arrow on the `Open` button and select `Open With...`, then select `Binary Editor`. – Lance U. Matthews Jul 14 '20 at 21:15