如何在文本文件中逐行运行 - 在Windows Mobile上?

时间:2011-01-06 20:50:32

标签: c# windows-mobile

在PC上的WinForm中,我使用这样运行:

FileStream FS = null;
StreamWriter SW = null;
FS = new FileStream(@"\Items.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
SW = new StreamWriter(FS, Encoding.Default);
while (SW.Peek() != -1)
{
   TEMP = (SW.ReadLine());
}

但是当我在Windows-mobile上尝试这个时,我得到错误:

Error   1   'System.IO.StreamWriter' does not contain a definition for 'Peek' and no extension method 'Peek' accepting a first argument of type 'System.IO.StreamWriter' could be found (are you missing a using directive or an assembly reference?)
Error   2   'System.IO.StreamWriter' does not contain a definition for 'ReadLine' and no extension method 'ReadLine' accepting a first argument of type 'System.IO.StreamWriter' could be found (are you missing a using directive or an assembly reference?)

怎么做?

感谢

4 个答案:

答案 0 :(得分:4)

如果您想阅读某些内容,请使用Reader而不是Writer

答案 1 :(得分:1)

你的意思是这样的吗?

using (var reader = File.OpenText("\\items.txt"))
{
    while(reader.Peek() > 0)
    {
        var line = reader.ReadLine();
        // do something with the line here
    }
}

我无法看到你的代码如何在桌面上工作,因为在StreamWriter上,ReadLine和Peek都不存在。

答案 2 :(得分:1)

如前所述,您使用的是streamwriter而不是streamreader

using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }

答案 3 :(得分:0)

试试这个,读取字符串中的所有数据。然后使用\ n拆分字符串,然后读取字符串数组中的每个值。