zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }

    运行效果图如下:

  • 相关阅读:
    准备开始学习XNA
    徐家骏:华为十年感悟
    memcached详解
    sql时间
    Sql server log file 缩小和删除
    看高手都是运用的灵活自如,打算从今天开始学习他!
    什么是内存对齐
    VS 2008 远程调试 与asp.net
    XNA入门的代码注释
    HTML的段落与文字
  • 原文地址:https://www.cnblogs.com/china_x01/p/1896764.html
Copyright © 2011-2022 走看看