1个回答
展开全部
byte
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 文件处理
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.TextBox txtDest;
private System.Windows.Forms.ProgressBar procNumber;
private System.Windows.Forms.Button btnDo;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ListBox lstInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtSource = new System.Windows.Forms.TextBox();
this.txtDest = new System.Windows.Forms.TextBox();
this.procNumber = new System.Windows.Forms.ProgressBar();
this.btnDo = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.lstInfo = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 0;
this.label1.Text = "源文件路径:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.TabIndex = 1;
this.label2.Text = "目标文件路径:";
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(160, 11);
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(360, 21);
this.txtSource.TabIndex = 2;
this.txtSource.Text = "";
//
// txtDest
//
this.txtDest.Location = new System.Drawing.Point(160, 42);
this.txtDest.Name = "txtDest";
this.txtDest.Size = new System.Drawing.Size(360, 21);
this.txtDest.TabIndex = 3;
this.txtDest.Text = "";
//
// procNumber
//
this.procNumber.Location = new System.Drawing.Point(24, 88);
this.procNumber.Name = "procNumber";
this.procNumber.Size = new System.Drawing.Size(360, 24);
this.procNumber.TabIndex = 4;
//
// btnDo
//
this.btnDo.Location = new System.Drawing.Point(408, 88);
this.btnDo.Name = "btnDo";
this.btnDo.Size = new System.Drawing.Size(112, 24);
this.btnDo.TabIndex = 5;
this.btnDo.Text = "执行合并";
this.btnDo.Click += new System.EventHandler(this.btnDo_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(416, 328);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(104, 23);
this.btnExit.TabIndex = 7;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lstInfo
//
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(24, 136);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawing.Size(496, 184);
this.lstInfo.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(544, 365);
this.Controls.Add(this.lstInfo);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDo);
this.Controls.Add(this.procNumber);
this.Controls.Add(this.txtDest);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void AddListItem(string Value)
{
lstInfo.Items.Add(Value);
lstInfo.Refresh();
}
private void btnDo_Click(object sender, System.EventArgs e)
{
string strSrc;
string strDest;
string tempdata;
int ii;
int Num;
int TotalNum;
strSrc = txtSource.Text.Trim();
strDest = txtDest.Text.Trim()+"\\ttttl.txt";
string[] dirs = Directory.GetFiles(strSrc,"*.txt");
TotalNum = dirs.GetUpperBound(0);
StreamWriter writer = new StreamWriter(strDest);
ii = 0;
Num = 0;
procNumber.Visible = true;
foreach(string dir in dirs)
{
Num = Num+1;
AddListItem("合并文件"+dir+".......");
StreamReader reader = new StreamReader(dir);
try
{
do
{
tempdata = reader.ReadLine();
tempdata = tempdata.Trim();
writer.WriteLine(tempdata);
}while(reader.Peek() != -1);
ii = ii+1;
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
reader.Close();
procNumber.Visible = false;
}
procNumber.Value = Num/TotalNum*100;
procNumber.Refresh();
}
procNumber.Visible = false;
AddListItem("合并操作结束");
AddListItem("本次操作共有"+ii.ToString()+"个文件合并到文件到文件"+strDest+"中去");
writer.Close();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 文件处理
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.TextBox txtDest;
private System.Windows.Forms.ProgressBar procNumber;
private System.Windows.Forms.Button btnDo;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ListBox lstInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtSource = new System.Windows.Forms.TextBox();
this.txtDest = new System.Windows.Forms.TextBox();
this.procNumber = new System.Windows.Forms.ProgressBar();
this.btnDo = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.lstInfo = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 0;
this.label1.Text = "源文件路径:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.TabIndex = 1;
this.label2.Text = "目标文件路径:";
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(160, 11);
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(360, 21);
this.txtSource.TabIndex = 2;
this.txtSource.Text = "";
//
// txtDest
//
this.txtDest.Location = new System.Drawing.Point(160, 42);
this.txtDest.Name = "txtDest";
this.txtDest.Size = new System.Drawing.Size(360, 21);
this.txtDest.TabIndex = 3;
this.txtDest.Text = "";
//
// procNumber
//
this.procNumber.Location = new System.Drawing.Point(24, 88);
this.procNumber.Name = "procNumber";
this.procNumber.Size = new System.Drawing.Size(360, 24);
this.procNumber.TabIndex = 4;
//
// btnDo
//
this.btnDo.Location = new System.Drawing.Point(408, 88);
this.btnDo.Name = "btnDo";
this.btnDo.Size = new System.Drawing.Size(112, 24);
this.btnDo.TabIndex = 5;
this.btnDo.Text = "执行合并";
this.btnDo.Click += new System.EventHandler(this.btnDo_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(416, 328);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(104, 23);
this.btnExit.TabIndex = 7;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lstInfo
//
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(24, 136);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawing.Size(496, 184);
this.lstInfo.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(544, 365);
this.Controls.Add(this.lstInfo);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDo);
this.Controls.Add(this.procNumber);
this.Controls.Add(this.txtDest);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void AddListItem(string Value)
{
lstInfo.Items.Add(Value);
lstInfo.Refresh();
}
private void btnDo_Click(object sender, System.EventArgs e)
{
string strSrc;
string strDest;
string tempdata;
int ii;
int Num;
int TotalNum;
strSrc = txtSource.Text.Trim();
strDest = txtDest.Text.Trim()+"\\ttttl.txt";
string[] dirs = Directory.GetFiles(strSrc,"*.txt");
TotalNum = dirs.GetUpperBound(0);
StreamWriter writer = new StreamWriter(strDest);
ii = 0;
Num = 0;
procNumber.Visible = true;
foreach(string dir in dirs)
{
Num = Num+1;
AddListItem("合并文件"+dir+".......");
StreamReader reader = new StreamReader(dir);
try
{
do
{
tempdata = reader.ReadLine();
tempdata = tempdata.Trim();
writer.WriteLine(tempdata);
}while(reader.Peek() != -1);
ii = ii+1;
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
reader.Close();
procNumber.Visible = false;
}
procNumber.Value = Num/TotalNum*100;
procNumber.Refresh();
}
procNumber.Visible = false;
AddListItem("合并操作结束");
AddListItem("本次操作共有"+ii.ToString()+"个文件合并到文件到文件"+strDest+"中去");
writer.Close();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询