1

Good day!

I try to get all lines from file.

Case: I write some strings into file (some WriteClass) and then try to get all lines frm it. via

var lines=System.IO.File.ReadAllLines(fileName,Encoding.Default);

Bit Count of lines==0! And i havent any exceptions.

What it can be?

Thank you!

user2545071
  • 1,408
  • 2
  • 24
  • 46

1 Answers1

0

I was having this issue as well. I know this is somewhat of a necrobump but what ultimately caused the issue was my URI was in the wrong format. I was treating it like a string and my parser was dutifully checking if the file existed before reading lines.

right:

XmlCsvReader reader = new XmlCsvReader(new Uri("file:///C:/Users/Z/Documents/test.csv"), doc.NameTable);

wrong:

XmlCsvReader reader = new XmlCsvReader(new Uri("C:\\Users\\Z\\Documents\\test.csv"), doc.NameTable);

Since the URI would never be valid, 'lines' would never be initialized. I'm guessing this may be the case with your issue. Parser example below.

            if (File.Exists(location.AbsolutePath))
        { //this will never run if the URI is formatted wrong
          //The file will never be found

            lines = File.ReadAllLines(location.AbsolutePath);
            Console.WriteLine(lines);
            index = 0;
            column = 0;
            depth = 0;
            tag = 0;
            rs = ReadState.Initial;
        }
Atomhax
  • 103
  • 2
  • 8
  • Never call File.Exists() in the first place. Just try to open the file and handle the exception if it fails. You need the exception handler anyway, because the file system is volatile (state can change in between when you check Exists() and try to use the file) and because File.Exists() doesn't account for things like permissions or locks. And once you have the exception handler, File.Exists() becomes extremely wasteful (extra trip to disk, which is almost THE slowest thing a computer can do) and redundant. – Joel Coehoorn Jun 27 '16 at 01:33