c# 正则搜索匹配+ 读取txt +OpenFileDialog+进度条progressBar应用实例 绝对原创

分类: code | 标签: | 日期:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
public string doc,keyword,start,end,st,et,sm,em;

void dataread()
{

FileStream fs = new FileStream(doc, FileMode.Open, FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
this.richTextBox1.Text = “”;
string strLine = m_streamReader.ReadLine();

while (strLine != null)
{

Regex r = new Regex(keyword); // 定义一个Regex对象实例
Match m = r.Match(strLine);

if (m.Success)
{

progressBar1.Minimum = 0;
progressBar1.Maximum++;
progressBar1.Value++;

this.richTextBox1.Text += strLine + “\n”;

}

progressBar1.Value = progressBar1.Maximum;

strLine = m_streamReader.ReadLine();

}

//关闭此StreamReader对象
m_streamReader.Close();
}

private void textBox1_Click(object sender, EventArgs e)
{
OpenFileDialog fDilag = new OpenFileDialog();
fDilag.Title = “选择要分析的日志文件(apache、iis、nginx等服务器日志全部兼容!)”;
fDilag.InitialDirectory = @”d:\”; \\ 有了@ 就不用在路径上加\\了!
fDilag.Filter = “服务器日志 (*.log)|*.log”;
fDilag.FilterIndex = 2;
fDilag.RestoreDirectory = true;
if (fDilag.ShowDialog() == DialogResult.OK)
{
doc = fDilag.FileName;
textBox1.Text = “日志加载成功!可以进行分析…”;
}
}

private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value=0;
if (doc != null)
{
keyword = comboBox1.Text;
dataread();
}
else
{
MessageBox.Show(“请选择正确的日志文件!”);
}
}

}
}

声明:本文为耿师傅 http://www.bordf.com发布文章。转载务必注明出处

注意:转载须保留全文:请保留本文出处!否则耿师傅将向你网站的主机商投诉。
本文永久地址:http://www.bordf.com/article/259.html


表情:<( ̄︶ ̄)> | (⊙ˍ⊙) | >﹏< | b( ̄▽ ̄)d | (─.─||) | (^_-)

[ Ctrl+Enter提交 ]