1.先拖一个按钮到主界面里,如下图
2.添加一个Click事件(或者直接双击按钮)
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> ZZZString = new List<string>();//定义一个集合,用于储存数据
//创建一个Click事件,用于打开读取文件
private void button1_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.OpenFileDialog objOpenFile = new System.Windows.Forms.OpenFileDialog();
objOpenFile.Filter = "文本文档|*.txt";
if (objOpenFile.ShowDialog() == DialogResult.OK)
{
//this.txtFile.Text = objOpenFile.FileName;
string[] ReadText = File.ReadAllLines(objOpenFile.FileName, Encoding.Default);
foreach (string item in ReadText)
{
if (item.ToUpper().Contains("ZZZ"))//搜索指定字符串ZZZ
{
ZZZString.Add(item);//搜索到的内容存入ZzzString集合
//richTextBox1.Text += item.ToString() + "\r\n";//添加一个richTextBox,把//去掉可看见搜索结果
//label1.Text = ZzzString.Count + "条";//添加一个label,把//去掉可看见数据总条数
}
}
}
}
}
}