CombineFile

制作电子书, 经常要合并文本文件, 而现有编辑软件却无此功能, 故写了一个 CombineFile 小程序.

 1. 这只是一个简单的 UTF-8 (*.txt)文本文件合并工具,勿作它用。

 2. 选择已存在的文件作为保存目标时,已存在的文件将被删除。

 3. 在合并前, 将文件以数字开头排序, 可省不少麻烦。

又,该程序的源代码如下:

CombineFileForm
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CombineFile
{
    public partial class CombineFileForm : Form
    {
        public CombineFileForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text files(*.txt)|*.txt|All Files(*.*)|*.*";
            dlg.Multiselect = true;

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string filename in dlg.FileNames)
                {
                    lstFile.Items.Add(filename);
                }
            }

            SetButton();
        }

        private void SetButton()
        {
            if (lstFile.Items.Count > 0)
            {
                btnRemove.Enabled = true;
                btnCombine.Enabled = true;
            }
            else
            {
                btnRemove.Enabled = false;
                btnCombine.Enabled = false;
            }
        }

        private void btnCombine_Click(object sender, EventArgs e)
        {
            string path;

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                path = dlg.FileName;
            }
            else
            {
                return;
            }

            foreach (string item in lstFile.Items)
            {
                if (item == path)
                {
                    MessageBox.Show("Combined-file and saved-path cannot be same.");
                    return;
                }
            }

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            
            foreach (string item in lstFile.Items)
            {
                string s = Read(item);
                File.AppendAllText(path, s);
            }

            MessageBox.Show("Combine success!");
        }

        /// <summary>
        /// 解决 ANSI 到 UTF8 的乱码问题。
        /// </summary>
        public string Read(string path)
        {
            string s;
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                s = sr.ReadToEnd();
            }
            return s;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            int count = lstFile.SelectedItems.Count;
            for (int i = 0; i < count; i++)
            {
                lstFile.Items.Remove(lstFile.SelectedItems[lstFile.SelectedItems.Count - 1]);
            }
            SetButton();
        }
    }
}

运行效果图如下:

 

posted on 2010-12-05 09:25  x01  阅读(182)  评论(0编辑  收藏  举报

导航