zoukankan      html  css  js  c++  java
  • 基于模版文件复制替换的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);
            }
        }
    }
  • 相关阅读:
    ELK原理以及一些处理难点分析
    mysql无法启动,Error: page 13476 log sequence number
    Linux lsattr命令
    mysql主从复制案例及小结
    Nagios
    iptables路由转发及控制
    DNS域名解析
    无法启动Print Spooler服务,错误代码1068,依赖服务或组件
    云计算虚拟化知识
    文件上传漏洞
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13112296.html
Copyright © 2011-2022 走看看