文件合并
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
namespace 文件合并一
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Button1_Click(object sender, EventArgs e)
{
string strSrc;//源文件路径
string strDest;//目标文件路径
string tempdata;
int ii;
int Num;
int TotalNum;
strSrc = TextBox1.Text.Trim();
strDest = TextBox2.Text.Trim()+"total.txt";
//获取windirl路径下所有文件列表
string[] dirs = Directory.GetFiles(strSrc,"*.txt");
//文件总数
TotalNum = dirs.GetUpperBound(0);
//建立或打开目标文件,准备读
StreamWriter sw = new StreamWriter(strDest);
ii = 0;
Num = 0;
ProgressBar1.Visible = true;
foreach (string dir in dirs)
{
Num += 1;
this.Add("合并文件"+dir+".......");
StreamReader sr = new StreamReader(dir);
try
{
do
{
tempdata = sr.ReadLine().Trim();
sw.WriteLine(tempdata);
}
while (sr.Peek() != -1);
{
ii += 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sr.Close();
//ProgressBar1.Visible = false;
}
ProgressBar1.Value = Num / TotalNum * 100;
ProgressBar1.Refresh();
}
//ProgressBar1.Visible = false;
//操作结束信息
Add("合并操作结束");
Add("本次操作公有"+ii.ToString()+"个文件合并到文件total.txt中去");
sw.Close();
}
//列表框显示信息方法
private void Add(string value)
{
listBox1.Items.Add(value);
listBox1.Refresh();
}
}
}
进取条老是不统一,可能这里的进取条又要用线程来做,让它成为后台运行,各位可以试一试