首页 > Windows开发 > 详细

用winform实现一个代码打包工具

时间:2019-01-21 19:05:20      阅读:150      评论:0      收藏:0      [点我收藏+]

一个net程序员必须拥有的能力就是可以随时随地写出一个自己需要的小工具,于是记录一下我的个人工具吧。

新建一个窗体应用项目,代码如下:

namespace 打包工具
{
    partial class 代码打包工具
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.Label label2;
            this.panel2 = new System.Windows.Forms.Panel();
            this.checkBox3 = new System.Windows.Forms.CheckBox();
            this.checkBox4 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.Package_Btn = new System.Windows.Forms.Button();
            this.PackageOpen_Btn = new System.Windows.Forms.Button();
            this.OpenPackageFileUrl = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.OpenFile_Btn = new System.Windows.Forms.Button();
            this.OpenFileUrl = new System.Windows.Forms.TextBox();
            this.AsOfTheDate = new System.Windows.Forms.DateTimePicker();
            this.panel3 = new System.Windows.Forms.Panel();
            this.Show_Box = new System.Windows.Forms.RichTextBox();
            this.checkBox5 = new System.Windows.Forms.CheckBox();
            this.checkBox6 = new System.Windows.Forms.CheckBox();
            label2 = new System.Windows.Forms.Label();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Location = new System.Drawing.Point(36, 59);
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size(89, 12);
            label2.TabIndex = 7;
            label2.Text = "忽略文件格式:";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.checkBox6);
            this.panel2.Controls.Add(this.checkBox5);
            this.panel2.Controls.Add(this.checkBox3);
            this.panel2.Controls.Add(this.checkBox4);
            this.panel2.Controls.Add(this.checkBox2);
            this.panel2.Controls.Add(this.checkBox1);
            this.panel2.Controls.Add(this.Package_Btn);
            this.panel2.Controls.Add(label2);
            this.panel2.Controls.Add(this.PackageOpen_Btn);
            this.panel2.Controls.Add(this.OpenPackageFileUrl);
            this.panel2.Controls.Add(this.label1);
            this.panel2.Controls.Add(this.OpenFile_Btn);
            this.panel2.Controls.Add(this.OpenFileUrl);
            this.panel2.Controls.Add(this.AsOfTheDate);
            this.panel2.Location = new System.Drawing.Point(12, 175);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(776, 132);
            this.panel2.TabIndex = 1;
            // 
            // checkBox3
            // 
            this.checkBox3.AutoSize = true;
            this.checkBox3.Location = new System.Drawing.Point(368, 58);
            this.checkBox3.Name = "checkBox3";
            this.checkBox3.Size = new System.Drawing.Size(48, 16);
            this.checkBox3.TabIndex = 16;
            this.checkBox3.Text = ".pdb";
            this.checkBox3.UseVisualStyleBackColor = true;
            this.checkBox3.CheckedChanged += new System.EventHandler(this.AddGetNoPackageFormatList);
            // 
            // checkBox4
            // 
            this.checkBox4.AutoSize = true;
            this.checkBox4.Location = new System.Drawing.Point(306, 58);
            this.checkBox4.Name = "checkBox4";
            this.checkBox4.Size = new System.Drawing.Size(42, 16);
            this.checkBox4.TabIndex = 15;
            this.checkBox4.Text = ".cs";
            this.checkBox4.UseVisualStyleBackColor = true;
            this.checkBox4.CheckedChanged += new System.EventHandler(this.AddGetNoPackageFormatList);
            // 
            // checkBox2
            // 
            this.checkBox2.AutoSize = true;
            this.checkBox2.Location = new System.Drawing.Point(220, 58);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(66, 16);
            this.checkBox2.TabIndex = 14;
            this.checkBox2.Text = ".csproj";
            this.checkBox2.UseVisualStyleBackColor = true;
            this.checkBox2.CheckedChanged += new System.EventHandler(this.AddGetNoPackageFormatList);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(133, 58);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(66, 16);
            this.checkBox1.TabIndex = 13;
            this.checkBox1.Text = ".config";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new System.EventHandler(this.AddGetNoPackageFormatList);
            // 
            // Package_Btn
            // 
            this.Package_Btn.BackColor = System.Drawing.Color.Red;
            this.Package_Btn.Cursor = System.Windows.Forms.Cursors.Hand;
            this.Package_Btn.Enabled = false;
            this.Package_Btn.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Package_Btn.ForeColor = System.Drawing.SystemColors.Control;
            this.Package_Btn.Location = new System.Drawing.Point(36, 82);
            this.Package_Btn.Name = "Package_Btn";
            this.Package_Btn.Size = new System.Drawing.Size(711, 41);
            this.Package_Btn.TabIndex = 12;
            this.Package_Btn.Text = "打包";
            this.Package_Btn.UseVisualStyleBackColor = false;
            this.Package_Btn.Click += new System.EventHandler(this.Package_Btn_Click);
            // 
            // PackageOpen_Btn
            // 
            this.PackageOpen_Btn.Location = new System.Drawing.Point(672, 28);
            this.PackageOpen_Btn.Name = "PackageOpen_Btn";
            this.PackageOpen_Btn.Size = new System.Drawing.Size(75, 23);
            this.PackageOpen_Btn.TabIndex = 6;
            this.PackageOpen_Btn.Text = "打包文件夹";
            this.PackageOpen_Btn.UseVisualStyleBackColor = true;
            this.PackageOpen_Btn.Click += new System.EventHandler(this.PackageOpen_Btn_Click);
            // 
            // OpenPackageFileUrl
            // 
            this.OpenPackageFileUrl.Location = new System.Drawing.Point(342, 30);
            this.OpenPackageFileUrl.Name = "OpenPackageFileUrl";
            this.OpenPackageFileUrl.ReadOnly = true;
            this.OpenPackageFileUrl.Size = new System.Drawing.Size(313, 21);
            this.OpenPackageFileUrl.TabIndex = 5;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(36, 33);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = "打包时间:";
            // 
            // OpenFile_Btn
            // 
            this.OpenFile_Btn.Location = new System.Drawing.Point(672, 3);
            this.OpenFile_Btn.Name = "OpenFile_Btn";
            this.OpenFile_Btn.Size = new System.Drawing.Size(75, 23);
            this.OpenFile_Btn.TabIndex = 3;
            this.OpenFile_Btn.Text = "选择文件夹";
            this.OpenFile_Btn.UseVisualStyleBackColor = true;
            this.OpenFile_Btn.Click += new System.EventHandler(this.OpenFile_Btn_Click);
            // 
            // OpenFileUrl
            // 
            this.OpenFileUrl.Location = new System.Drawing.Point(36, 3);
            this.OpenFileUrl.Name = "OpenFileUrl";
            this.OpenFileUrl.ReadOnly = true;
            this.OpenFileUrl.Size = new System.Drawing.Size(619, 21);
            this.OpenFileUrl.TabIndex = 2;
            // 
            // AsOfTheDate
            // 
            this.AsOfTheDate.Location = new System.Drawing.Point(107, 30);
            this.AsOfTheDate.Name = "AsOfTheDate";
            this.AsOfTheDate.Size = new System.Drawing.Size(200, 21);
            this.AsOfTheDate.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.Show_Box);
            this.panel3.Location = new System.Drawing.Point(12, 12);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(776, 157);
            this.panel3.TabIndex = 2;
            // 
            // Show_Box
            // 
            this.Show_Box.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Show_Box.Location = new System.Drawing.Point(0, 0);
            this.Show_Box.Name = "Show_Box";
            this.Show_Box.Size = new System.Drawing.Size(776, 157);
            this.Show_Box.TabIndex = 0;
            this.Show_Box.Text = "";
            // 
            // checkBox5
            // 
            this.checkBox5.AutoSize = true;
            this.checkBox5.Location = new System.Drawing.Point(482, 60);
            this.checkBox5.Name = "checkBox5";
            this.checkBox5.Size = new System.Drawing.Size(48, 16);
            this.checkBox5.TabIndex = 17;
            this.checkBox5.Text = ".pdb";
            this.checkBox5.UseVisualStyleBackColor = true;
            // 
            // checkBox6
            // 
            this.checkBox6.AutoSize = true;
            this.checkBox6.Location = new System.Drawing.Point(422, 59);
            this.checkBox6.Name = "checkBox6";
            this.checkBox6.Size = new System.Drawing.Size(54, 16);
            this.checkBox6.TabIndex = 18;
            this.checkBox6.Text = ".user";
            this.checkBox6.UseVisualStyleBackColor = true;
            // 
            // 代码打包工具
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 319);
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel2);
            this.Name = "代码打包工具";
            this.Text = "代码打包工具";
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Button Package_Btn;
        private System.Windows.Forms.Button PackageOpen_Btn;
        private System.Windows.Forms.TextBox OpenPackageFileUrl;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button OpenFile_Btn;
        private System.Windows.Forms.TextBox OpenFileUrl;
        private System.Windows.Forms.DateTimePicker AsOfTheDate;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.RichTextBox Show_Box;
        private System.Windows.Forms.CheckBox checkBox3;
        private System.Windows.Forms.CheckBox checkBox4;
        private System.Windows.Forms.CheckBox checkBox2;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.CheckBox checkBox6;
        private System.Windows.Forms.CheckBox checkBox5;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 打包工具
{
    public partial class 代码打包工具 : Form
    {
        /// <summary>
        /// 打包文件存储位置
        /// </summary>
        private static string OpenPackageFolder = string.Empty;
        /// <summary>
        /// 需打包文件位置
        /// </summary>
        private static string PackageFolder = string.Empty;
        /// <summary>
        /// 不参与打包的文件格式
        /// </summary>
        private static List<string> noPackageFormatList = new List<string>();
        public 代码打包工具()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 选择需要打包的文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenFile_Btn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.OpenFileUrl.Text = dialog.SelectedPath;
                PackageFolder = dialog.SelectedPath;
                ShowTime_Box("成功获取打包文件夹。");
                if (!string.IsNullOrEmpty(OpenPackageFolder))
                {
                    Package_Btn.Enabled = true;
                    Package_Btn.BackColor = System.Drawing.Color.Green;
                }

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PackageOpen_Btn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.OpenPackageFileUrl.Text = dialog.SelectedPath;
                OpenPackageFolder = dialog.SelectedPath;
                ShowTime_Box("成功获取打包后保存文件夹。");
                if (!string.IsNullOrEmpty(PackageFolder))
                {
                    Package_Btn.Enabled = true;
                    Package_Btn.BackColor = System.Drawing.Color.Green;
                }
            }
        }

        private void Package_Btn_Click(object sender, EventArgs e)
        {
            var date = AsOfTheDate.Value;
            ShowTime_Box("正在打包,请稍后……");
            CopyDirectory(PackageFolder,OpenPackageFolder, date);
            DeleteEmptyDir(OpenPackageFolder,true);
            ShowTime_Box("打包完成。");
        }
        private void AddGetNoPackageFormatList(object sender, EventArgs e)
        {
            try {
                var obj = (CheckBox)sender;
                if (!noPackageFormatList.Contains(obj.Text.ToLower()))
                    noPackageFormatList.Add(obj.Text.ToLower());
                else
                    noPackageFormatList.Remove(obj.Text.ToLower());
            }
            catch (Exception ex) {

            }
            
        }
        public void ShowTime_Box(string msg)
        {
            Invoke(new Action(delegate {
                Show_Box.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\r\n" + msg + "\r\n");
                Show_Box.Focus();//获取焦点
                Show_Box.Select(Show_Box.TextLength, 0);//光标定位到文本最后
                Show_Box.ScrollToCaret();//滚动到光标处
            }));
        }
        public static void CopyDirectory(string srcPath, string destPath,DateTime date)
        {
            try
            {
                var dir = new DirectoryInfo(srcPath);
                var fileinfo = dir.GetFileSystemInfos();  //获取目录下(不包含子目录)的文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     //判断是否文件夹
                    {
                        if (!Directory.Exists(destPath + "\\" + i.Name))
                        {
                            Directory.CreateDirectory(destPath + "\\" + i.Name);   //目标目录下不存在此文件夹即创建子文件夹
                        }
                        CopyDirectory(i.FullName, destPath + "\\" + i.Name, date);    //递归调用复制子文件夹
                    }
                    else
                    {
                        if(!noPackageFormatList.Contains(i.Extension.ToLower()) && date <= i.LastWriteTime)
                            File.Copy(i.FullName, destPath + "\\" + i.Name, true);      //不是文件夹即复制文件,true表示可以覆盖同名文件
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
        private static void DeleteEmptyDir(string path,bool isTop)
        {
            string[] dirs = Directory.GetDirectories(path);
            for (int i = 0; i < dirs.Length; i++)
            {
                DeleteEmptyDir(dirs[i],false);
            }
            try
            {
                if(!isTop)
                Directory.Delete(path);
            }
            catch (Exception e)
            {
                //Debug.Log("无法删除,因为文件夹不为空:"+path);
            }
        }
    }
}

 

用winform实现一个代码打包工具

原文:https://www.cnblogs.com/yuchenghao/p/10299944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!