码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-13 12:43:36      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:调用   vat   each   names   init   lin   from   draw   star   

功能分析

将源目录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代码生成器(一)

标签:调用   vat   each   names   init   lin   from   draw   star   

原文地址:https://www.cnblogs.com/xiewenyu/p/13112296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!