c#如何实现文件的分解及合并

2025-10-20 17:35:13

1、首先打开vs2013(其他版本也可以),在C#项目中选择 Windows窗体应用程序。 写上项目名字点创建。(本例用VS2013系统默认项目名,路径随意。)

c#如何实现文件的分解及合并

2、创建项目后,可以看到一个窗口,将Form1窗口的Text属性改为"分解合并文件工具"。在工具箱中拖二个“Button”按钮、二个“TexBox”文本框、三个“Label”和一个“ListBox”调整界面如下图。

c#如何实现文件的分解及合并

3、修改代码:

        public Form1()

        {

            InitializeComponent();

            listBox1.AllowDrop = true;

        }

c#如何实现文件的分解及合并

4、添加listBox1拖动代码:

      private void listBox1_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))

                e.Effect = DragDropEffects.All;

            else

                e.Effect = DragDropEffects.None;

        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)

        {

            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            int i;

            for (i = 0; i < s.Length; i++)

            {

                if (s[i].Trim() != "")

                {

                    listBox1.Items.Add(s[i]);

                }

            }

        }

c#如何实现文件的分解及合并

5、添加合并文件操作,双击窗口中“合并文件”按钮。

        private void button1_Click(object sender, EventArgs e)

        {

            string[] s =new string[ listBox1.Items.Count];

            int i;

            for (i = 0; i < s.Length; i++)

            {         

                 s[i]= listBox1.Items[i] as string ;               

            }

            string 合并文件名 = this.textBox1.Text.Trim();

            合并文件(s, 合并文件名);

        }

c#如何实现文件的分解及合并

6、添加分解文件操作,双击窗口中“分解文件”按钮。

        private void button2_Click(object sender, EventArgs e)

        {

            //分解文件只取listBox1控件中第一个文件名

            string s = listBox1.Items[0] as string;

            int n = Convert.ToInt32(this.textBox2.Text.Trim());

            分解文件(s,n);

        }

c#如何实现文件的分解及合并

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢