基于模版文件复制替换的abpcore代码生成器

Posted xiewenyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于模版文件复制替换的abpcore代码生成器相关的知识,希望对你有一定的参考价值。

功能分析

将源目录Source的文件复制到目录Target中并替换文件名和文件内容

效果图

技术图片

 

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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ABPGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog1.SelectedPath;
                
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            lblMsg.Text = "";
            if (string.IsNullOrWhiteSpace(txtInput.Text))
            {
                MessageBox.Show("输入值不得为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtOutput.Text))
            {
                MessageBox.Show("输入值不得为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtFolder.Text))
            {
                MessageBox.Show("文件夹目录不得为空");
                return;
            }

            string sourcePath = txtFolder.Text;
            string targetPath = txtFolder.Text.Replace("Source","Target");
            CopyDirectory(sourcePath, targetPath,txtInput.Text,txtOutput.Text);

            ReplaceStrFile(targetPath, txtInput.Text, txtOutput.Text);
            lblMsg.Text = "生成成功";

        }

        private void ReplaceStrFile(string path,string from, string to)
        {
            
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                string strFilePath = fi.FullName;
                string strContent = File.ReadAllText(strFilePath,Encoding.UTF8);
                strContent = Regex.Replace(strContent, from, to);
                strContent = Regex.Replace(strContent, from.ToLower(), to.ToLower());
                File.WriteAllText(strFilePath, strContent,Encoding.UTF8);

            }
            DirectoryInfo[] dis = di.GetDirectories();
            foreach (DirectoryInfo item in dis)
            {
                this.ReplaceStrFile(item.FullName,from,to);
            }
        }

        private void CopyDirectory(string srcPath, string destPath,string from,string to)
        {
            
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //获取目录下(不包含子目录)的文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     //判断是否文件夹
                    {
                        string newFolderName= i.Name.Replace(from, to);
                        if (!Directory.Exists(destPath + "\\" + newFolderName))
                        {
                            Directory.CreateDirectory(destPath + "\\" + newFolderName);   //目标目录下不存在此文件夹即创建子文件夹
                        }
                        CopyDirectory(i.FullName, destPath + "\\" + newFolderName, from ,to);    //递归调用复制子文件夹
                    }
                    else
                    {
                        string newfileName = i.Name.Replace(from,to);
                        File.Copy(i.FullName, destPath + "\\" + newfileName, true);      //不是文件夹即复制文件,true表示可以覆盖同名文件
                    }
                }
            }
            catch (Exception e)
            {
                lblMsg.Text = "生成失败";
                throw;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtFolder.Text = GetApplicationPath()+"output\\Source";
        }
        private static string GetApplicationPath()
        {
            string path = Application.StartupPath;
            string folderName = String.Empty;
            while (folderName.ToLower() != "bin")
            {
                path = path.Substring(0, path.LastIndexOf("\\"));
                folderName = path.Substring(path.LastIndexOf("\\") + 1);
            }
            return path.Substring(0, path.LastIndexOf("\\") + 1);
        }
    }
}

 

以上是关于基于模版文件复制替换的abpcore代码生成器的主要内容,如果未能解决你的问题,请参考以下文章

基于数据库的vs2019的T4模版代码生成器基于mysql数据库

基于数据库的vs2019的T4模版代码生成器基于sqlserver数据库

Ansible详解

POI 按word模版生成合同并生成PDF

poi根据模版生成多页word,并压缩下载

java动态生成word,该怎么解决