文件合并

来源:互联网 发布:k歌软件 mtv 编辑:程序博客网 时间:2024/04/28 22:16
<script type="text/javascript"><!--google_ad_client = "pub-9528830580198364";/* 文章顶部广告(方框)300&#42;250 */google_ad_slot = "0068799835";google_ad_width = 300;google_ad_height = 250;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 

作业:文件合并工具:c#代码


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
namespace splitfile
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.ListBox lissource;
        
private System.Windows.Forms.Button btnscan;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Button btndes;
        
private System.Windows.Forms.TextBox txtdes;
        
private System.Windows.Forms.OpenFileDialog opensource;
        
private System.Windows.Forms.OpenFileDialog opendes;
        
private System.Windows.Forms.Button btnmerge;
        
private System.Windows.Forms.ProgressBar prgdis;
        
private System.Windows.Forms.Label lbldis;
        
/// <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 );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }



        
void adddes()
        
{
            opendes.ShowDialog();
            txtdes.Text
=opendes.FileName;

        }


        
void additem()
        
{
            
string[] path;
            opensource.ShowDialog();
            path
=opensource.FileNames;
            
for(int i=0;i<path.Length;i++)
            
{
                lissource.Items.Add(path[i]);
            }


        }

        
void merge()
        
{
            
if(lissource.Items.Count==0)
            
{
                MessageBox.Show(
this,"源文件没有指定!","失败");
                
return;
            }

            
if(txtdes.Text.Equals(""))
            
{
                MessageBox.Show(
this,"目标文件没有指定!","失败");
                
return;
            }

            
string content;
            prgdis.Maximum
=lissource.Items.Count;
            
for(int i=0;i<lissource.Items.Count;i++)
            
{
                
string sourpath=@lissource.Items[i].ToString();
                StreamReader sr
=new StreamReader(sourpath,Encoding.Default,true,512);
                content
=sr.ReadToEnd();
                sr.Close();
                
string despath=@txtdes.Text;
                StreamWriter sw
=new StreamWriter(despath,true,Encoding.Default,512);
                sw.Flush();
                sw.Write(content);
                sw.Close();
                prgdis.Increment(
1);
                
if(prgdis.Value==prgdis.Maximum)
                
{
                    MessageBox.Show(
this,"合并成功!","成功");
                    lissource.Items.Clear();
                    txtdes.Text
="";
                    prgdis.Value
=0;
                    
return;
                }

            }

        }


        
private void btnscan_Click(object sender, System.EventArgs e)
        
{
            additem();

        }

    

        
private void btndes_Click(object sender, System.EventArgs e)
        
{
            adddes();
        }


        
private void btnmerge_Click(object sender, System.EventArgs e)
        
{
             merge();
        }

    
    }

}