C# 将text.txt文档的文本行输出到文本块

C# 将text.txt文档的文本行输出到文本块,c#,string,windows-phone-7,C#,String,Windows Phone 7,页面上的应用程序带有文本块和按钮,还涉及到.txt文件中的文本建议,每个建议放在同一行上,只有大约100行。单击按钮语句时,文档的第一个文本行显示在文本块中: public string GetQ() { string pathFile = "Q.txt"; Uri uri = new Uri(pathFile, UriKind.Relative); StreamResourceInfo sri = Application.GetReso

页面上的应用程序带有文本块和按钮,还涉及到.txt文件中的文本建议,每个建议放在同一行上,只有大约100行。单击按钮语句时,文档的第一个文本行显示在文本块中:

public string GetQ()
    {
        string pathFile = "Q.txt";
        Uri uri = new Uri(pathFile, UriKind.Relative); 
        StreamResourceInfo sri = Application.GetResourceStream(uri);
        using (StreamReader sr = new StreamReader(sri.Stream))
        {
            string wordline = sr.ReadLine();
            return wordline;
        }

    }
如何使您下次按下按钮时,出现文档的下一行


谢谢

这是未经测试的,但您可以将文件存储在字符串数组中,然后从中访问所需内容,而无需不断重新打开文件以读取每一行

var qFile = new List<string>();

public string GetQ()
{
    string pathFile = "Q.txt";
    Uri uri = new Uri(pathFile, UriKind.Relative); 
    StreamResourceInfo sri = Application.GetResourceStream(uri);
    using (StreamReader sr = new StreamReader(sri.Stream))
    {
        string line = "";
        while ((line != null)
        {
            line = sr.ReadLine());
            if (line != null)
                qFile.Add(line);  // Add to list
    }
}

现在您可以将qFile[0]加载到qFile[qFile.Count-1]。

这是未经测试的,但您可以将文件存储在字符串数组中,然后从中访问所需内容,而无需不断重新打开文件以读取每一行

var qFile = new List<string>();

public string GetQ()
{
    string pathFile = "Q.txt";
    Uri uri = new Uri(pathFile, UriKind.Relative); 
    StreamResourceInfo sri = Application.GetResourceStream(uri);
    using (StreamReader sr = new StreamReader(sri.Stream))
    {
        string line = "";
        while ((line != null)
        {
            line = sr.ReadLine());
            if (line != null)
                qFile.Add(line);  // Add to list
    }
}

现在,您只需将qFile[0]加载到qFile[qFile.Count-1]。

通过File.ReadLines即可轻松完成所需的操作,如我的几行代码所示,没有单元测试完成它

    private static int LineNumber = 0;
    private List<string> textLines = new List<string>();

    public string GetTextLine()
    {
        const string pathFile = @"C:\test\Q.txt";

        if (textLines.Count == 0)
        {
            textLines = File.ReadLines(pathFile).ToList();
        }

        if (LineNumber < (textLines.Count - 1))
        {
            return textLines[LineNumber++];
        }

        return textLines[LineNumber];
    }

希望它能帮你好运…

你想要的东西可以通过File.ReadLines轻松完成,如我快速编写的几行代码所示,没有完成单元测试

    private static int LineNumber = 0;
    private List<string> textLines = new List<string>();

    public string GetTextLine()
    {
        const string pathFile = @"C:\test\Q.txt";

        if (textLines.Count == 0)
        {
            textLines = File.ReadLines(pathFile).ToList();
        }

        if (LineNumber < (textLines.Count - 1))
        {
            return textLines[LineNumber++];
        }

        return textLines[LineNumber];
    }

希望它能帮你好运…

你能解释一下关于将qFile[0]加载到qFile[qFile.Count-1]的更多细节吗?谢谢您能解释一下关于将qFile[0]加载到qFile[qFile.Count-1]的更多细节吗?谢谢错误:“System.IO.File”不包含“ReadLines”的定义错误:“System.IO.File”不包含“ReadLines”的定义