本文介绍了System.IO.File.ReadAllLines返回零个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我尝试从文件中获取所有行.

I try to get all lines from file.

案例:我将一些字符串写入文件(某些WriteClass),然后尝试从它获取所有行.通过

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

行数== 0!而且我没有任何例外.

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

可能是什么?

谢谢!

推荐答案

我也遇到了这个问题.我知道这有点麻烦,但最终导致问题的原因是我的URI格式错误.我把它当作一个字符串来对待,并且我的解析器在读取行之前会尽职地检查文件是否存在.

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.

右:

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

错误:

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

由于URI永远不会有效,因此行"将永远不会被初始化.我猜测您的问题可能就是这种情况.下面的解析器示例.

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

这篇关于System.IO.File.ReadAllLines返回零个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:45