读取文本文件C#

时间:2013-09-30 13:46:22

标签: c# .net-2.0 windows-ce streamreader textreader

private void button1_Click(object sender, EventArgs e)
    {
        string fileLoc = @"c:\wms.txt";

        if (File.Exists(fileLoc))
        {
            using (TextReader tr = new StreamReader(fileLoc))
            {
                MessageBox.Show(tr.ReadLine());
            }
        }
    }

这在我创建Windows应用程序时非常有效。

当我在设备应用程序中使用相同的代码时 - Windows CE我收到错误:

enter image description here

使用:.Net 2.0,visual Studio 2005

3 个答案:

答案 0 :(得分:5)

您的设备没有c驱动器。取代

string fileLoc = @"c:\wms.txt";

string fileLoc = @"wms.txt";

根文件夹似乎会自动添加到您的路径\

答案 1 :(得分:3)

Windows CE没有drive letters的概念。你的路径应该是@"\wms.txt"

答案 2 :(得分:-1)

string fileLoc = @"c:\wms.txt";

string fileLoc = "c:\\wms.txt";