文档内容查找

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.Windows.Forms;
using System.Web;
using System.IO;

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

    private void Form1_Load(object sender, EventArgs e)
    {


    }


    /// <summary>
    /// 选择文件路径
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {

        openFileDialog1.ShowDialog();


        textBox2.Text = openFileDialog1.FileName;
    }


    /// <summary>
    /// 选择文件夹路径
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        textBox3.Text = folderBrowserDialog1.SelectedPath;
    }


    /// <summary>
    /// 执行操作,分离文档内容
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        this.listBox1.Items.Clear();//清除listbox内容
        List<string> ss = ReadTextFileToList(textBox2.Text);
        List<string> txt1List= new List<string>();
        List<string> txt2List = new List<string>();
        string path = textBox3.Text + "\\" + textBox1.Text + ".txt";
        int a = 1;
        int b = 1;
        for(int j=0;j<ss.Count;j++)
        {
            //判断字符串中是否存在指定字符串
            int of=ss[j].IndexOf(textBox4.Text);
            if (of==-1)
            {

                txt2List.Add(b + ":" + ss[j].ToString());
                b++;
            }
            else
            {
                txt1List.Add(a + ":" + ss[j].ToString());
                a++;
            }
        }
        //判断点选按钮是否被选中
        if (txtredio.Checked)
        {
            pathwrite(txt1List, path);

        }
        else if (txtredio2.Checked)
        {
            pathwrite(txt2List,path);
        }
        else
        {
            MessageBox.Show("未选择功能!");
        }

        MessageBox.Show("操作结束");




    }
    //#region 写入文件
    /// <summary>
    /// 写入文件 ,不存在直接创建
    /// </summary>
    /// <param name="patha">路径(包括文件名)</param>
    /// <param name="content">内容</param>
    public  void   pathwrite( List<string> polt ,string path)
    {
        listBox1.Items.Add("执行导出文件:"+textBox1.Text+".txt");
        listBox1.Items.Add("当前总共:" + polt.Count + "条");
        using (StreamWriter w = File.AppendText(path))
        {
            for (int i = 0; i < polt.Count; i++)
            {


                listBox1.Items.Add("操作日志:写入第" + (i + 1) + "/" + polt.Count + "");
                w.WriteLine(polt[i].ToString());
                w.Flush();  //清除此流的缓冲区,内容写入文本

            }

        }
        listBox1.Items.Add("写入结束");
    }



    /// <summary>
    /// 获取数据包数据,执行方法创建或更新数据
    /// </summary>
    /// <param name="path"></param>
    public List<string> ReadTextFileToList(string fileName)
    {
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        List<string> list = new List<string>();
        StreamReader sr = new StreamReader(fs, Encoding.Default);
        //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
        //使用StreamReader类来读取文件 
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        // 从数据流中读取每一行,直到文件的最后一行
        string tmp = sr.ReadLine();
        while (tmp != null)
        {
            list.Add(tmp);
            tmp = sr.ReadLine();
        }
        //关闭此StreamReader对象 
        sr.Close();
        fs.Close();
        MessageBox.Show("读取成功"); 
        return list;
    }




}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值