Пример #1
0
        /// <summary>
        /// 将某个表相关的整个模板加入参数进行生成
        /// </summary>
        /// <param name="SourcePath"></param>
        /// <param name="DestinationPath"></param>
        /// <param name="TableName"></param>
        public static void GenerateCodesInDir(string SourcePath, string DestinationPath, Tuple <string, string, string> t3)
        {
            string    tableName             = t3.Item1;
            string    tableConnectionString = t3.Item2;
            string    tableModel            = t3.Item3;
            string    tableDescription      = t3.Item3;
            DataTable tableInfo             = SQLServerDBHelper.GetTableInfo(tableConnectionString, tableName);
            //string
            string fieldCode = GenerateFieldCode(tableInfo);

            //创建所有文件夹
            foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
                                                                SearchOption.AllDirectories))
            {
                Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
            }

            //套用模板并且复制文件到新的备份目录
            foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
                                                          SearchOption.AllDirectories))
            {
                //文件模板套用
                string content = File.ReadAllText(newPath);
                content = content.Replace("##Author##", Dns.GetHostName())
                          .Replace("##DateTime##", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                          .Replace("##TableDescription##", tableModel)
                          .Replace("##ModelName##", tableModel)
                          .Replace("##Fields##", fieldCode)
                          .Replace("##TableName##", tableName);
                //替换文件名
                File.WriteAllText(newPath.Replace(SourcePath, DestinationPath).Replace("Order", tableModel)
                                  .Replace(".template", ".cs"), content);
            }
        }