【问题标题】:Read Text file and display results in separate Labels C# Visual Studios读取文本文件并在单独的标签中显示结果 C# Visual Studios
【发布时间】:2016-04-19 21:19:31
【问题描述】:

我希望从 exe 所在的文件夹中读取一个 .txt 文件,并在多个相应的标签或 RichtextBox 的表单中显示部分文本文件。

虚拟示例: 文本框内容:“b1.txt”

QuestionTitle="鸡为什么过马路";

AnswerTitle[0]="它没有";

AnswerTitle[1]="谁";

当我点击搜索以显示以下内容时,我想要的表单:

标签 1 - 问题:鸡为什么要过马路

标签 2 - 答案 1:它没有

标签 3 - 答案 2:谁在乎

我看到人们使用 StreamReader,而其他人使用 File.OpenRead。我不确定哪种方法是从文本文件中准备好文本并将文件中的特定文本显示到特定标签或 Richtextbox 的最佳方法。

任何帮助都会很棒!

【问题讨论】:

  • 如您所说,继续上述任何一项(StreamReader 和其他人使用 File.OpenRead),如果遇到任何问题,请返回
  • 对以上其中一项感到抱歉的是什么?哦,你的意思是 StreamReader 和或 File.OpenRead。我只看到他们拉了一个完整的文本文件。不是它的一部分,所以我在这里没有什么可做的。我可以提取整个文本文件,但不能提取上面提到的特定文本,所以我需要帮助。
  • @CurtisHumphreys 尝试将您的文本拆分为一个数组,然后一个一个地获取它们
  • @Virgil 对,所以我对这一切都很陌生。我无法更改文本文件的格式,因为这些文件是从另一个程序中提取的,因此它们必须保持该格式,我不确定将我的文本拆分为数组是什么意思。恐怕这个问题对这个论坛的新手来说很抱歉。
  • 基本上我想这样做,但使用 C# youtube.com/watch?v=e-HMR0ootSo。我可以弄清楚其余的......

标签: c# visual-studio-2010


【解决方案1】:

要对字符串进行拆分,您需要使用带有字符串数组的重载:

string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

如果您想处理文本中不同类型的换行符,您可以使用匹配多个字符串的功能。这将在任何一种换行符上正确拆分,并在文本中保留空行和间距:

string[] lines = theText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

然后执行 lines[0] 将得到您的问题,lines[1] 将是您的第一个答案,依此类推。

【讨论】:

  • Environment.NewLine 属性包含系统的默认换行符。例如,对于 Windows 系统,它将是 "\r\n"。
  • 啊,这是一种更紧凑的方式,谢谢!
【解决方案2】:

所以我把这一切都整理出来了,谢谢你的帮助。这是该怎么做。注意我只想读取文本文件的每一第二行,因此有大量的字符串是占位符。我在其他地方使用了其他文本。

块引用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;


namespace tester_import
{
public partial class Form1 : Form
{


    public Form1()

    {


        InitializeComponent();



    }

    private void qsearch_Click(object sender, EventArgs e)
    {

    }

    private void qsearchbtn_Click(object sender, EventArgs e)
    {
        OpenFileDialog of = new OpenFileDialog();
        of.ShowDialog();
        qsearchtxt.Text = of.FileName;

    }

    private void viewtxt_Click(object sender, EventArgs e)
    {
        string str1;
        string str2;
        string str3;
        string str4;
        string str5;
        string str6;
        string str7;
        string str8;
        string str9;
        string str10;
        string str11;
        string str12;
        string str13;
        string str14;
        string str15;
        string str16;
        string str17;
        string str18;
        string str19;
        string str20;
        string str21;

        string dir = System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().Location);

        StreamReader sr = new StreamReader(dir+"/" + minput.Text + ".txt");
        str1 = sr.ReadLine();
        str2 = sr.ReadLine();
        str3 = sr.ReadLine();
        str4 = sr.ReadLine();
        str5 = sr.ReadLine();
        str6 = sr.ReadLine();
        str7 = sr.ReadLine();
        str8 = sr.ReadLine();
        str9 = sr.ReadLine();
        str10 = sr.ReadLine();
        str11 = sr.ReadLine();
        str12 = sr.ReadLine();
        str13 = sr.ReadLine();
        str14 = sr.ReadLine();
        str15 = sr.ReadLine();
        str16 = sr.ReadLine();
        str17 = sr.ReadLine();
        str18 = sr.ReadLine();
        str19 = sr.ReadLine();
        str20 = sr.ReadLine();
        str21 = sr.ReadLine();

        question.Text = str1.Split('"')[1];
        answerone.Text = str2.Split('"')[1];
        answertwo.Text = str4.Split('"')[1];
        answerthree.Text = str6.Split('"')[1];
        answerfour.Text = str8.Split('"')[1];
        answerfive.Text = str10.Split('"')[1];
        answersix.Text = str12.Split('"')[1];
        answerseven.Text = str14.Split('"')[1];
        answereight.Text = str16.Split('"')[1];
        answernine.Text = str18.Split('"')[1];
        answer10.Text = str20.Split('"')[1];

        sr.Close();



        }
    }
}

【讨论】:

  • 请查看数组示例。 :)
猜你喜欢
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
相关资源
最近更新 更多