| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2468 人关注过本帖, 1 人收藏
标题:C#中的文件处理,文件合并程序!(源代码)
取消只看楼主 加入收藏
kevin87
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-7-26
收藏(1)
 问题点数:0 回复次数:0 
C#中的文件处理,文件合并程序!(源代码)
由于论坛不能上传附件,所以演示程序不能上传,
想要程序的可以到http://www.这里来下载,知识是不分论坛的,希望管理员不要误会,这样也是为了程序员可以多多交流,我们的目的是大家交流分享经验,绝无他意!!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
namespace UniteFile
{
    public partial class frmUniteFile : Form
    {
        int TotalNum ;
        int count;
        int num;
        string strSrc;
        string strDest;
        string tempData;
        public frmUniteFile()
        {
            InitializeComponent();
        }
        private void addListItem(string value)
        {
            lstInfo.Items.Add(value);
            lstInfo.Refresh();
        }
        private void btnExecuteAim_Click(object sender, EventArgs e)
        {
            if (this.txtFilePath.Text.Trim().Equals("") || this.txtAimPath.Text.Trim().Equals(""))
            {
                MessageBox.Show("请填写正确的路径");
            }
            else
            {
                //使用Directory对象来获得源文件夹的文本文件集合
                string[] dirs = Directory.GetFiles(this.txtFilePath.Text, "*.TXT");
                //文件总数
                TotalNum = dirs.GetUpperBound(0);
                //源文件路径
                strSrc = this.txtFilePath.Text.Trim();
                //目标文件路径
                strDest = this.txtAimPath.Text.Trim() + "[url=file://\\total.txt]\\total.txt[/url]";
                //建立或打开目标文件,准备读
                StreamWriter writer = new StreamWriter(strDest);
                //显示进度条
                procNumber.Visible = true;
                count = 0;
                num = 0;
                foreach (string dir in dirs)
                {
                    num = num + 1;
                    //lstInfo显示信息
                    addListItem("正在合并文件 ->" + dir + "......");
                    StreamReader reader = new StreamReader(dir);
                    try
                    {
                        do
                        {
                            tempData = reader.ReadLine();
                            tempData = tempData.Trim();
                            writer.WriteLine(tempData);
                        }
                        while (reader.Peek() != -1);
                        {
                            count = count + 1;
                            procNumber.Value = count / TotalNum * 100;
                    this.label1.Text =Convert.ToString(procNumber.Value);
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                    }
                    finally
                    {
                        reader.Close();
                        procNumber.Visible = true ;
                    }
                  
                    procNumber.Refresh();
                }
                procNumber.Visible = true ;
                addListItem("合并操作结束");
                addListItem("本次操作共有" + count + "个文件合并到文件Total.txt中去");
                writer.Flush();
                writer.Close();
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}


在源文件路径输入一个有txt格式文件的文件夹根目录,例如:c:\\txt\
目标文件夹输入要合并到哪个目录下面,例如D:\\
搜索更多相关主题的帖子: using System 
2008-07-26 01:41
快速回复:C#中的文件处理,文件合并程序!(源代码)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038061 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved