System.IO.File.ReadAllLines returns zero strings

∥☆過路亽.° 提交于 2020-01-05 08:20:59

问题


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!


回答1:


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;
        }


来源:https://stackoverflow.com/questions/22652809/system-io-file-readalllines-returns-zero-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!