标签:调用 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); } } }
标签:调用 vat each names init lin from draw star
原文地址:https://www.cnblogs.com/xiewenyu/p/13112296.html