222
schangxiang@126.com
2025-04-29 4352ad5cbeef498392178655cb367ee38e574178
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
using GenerateCode_GEBrilliantFactory;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace GenerateCode_GEBrilliantFactory
{
    public class TextHelper
    {
 
        /// <summary>
        /// 读取text文本内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadText(string path)
        {
            try
            {
                StreamReader sr = new StreamReader(path, Encoding.Default);
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = sr.ReadLine()) != null)
                {
                    sb.Append(line.ToString() + " \n");
                }
                sr.Close();
                sr.Dispose();
                return sb.ToString();
            }
            catch
            {
                throw;
            }
        }
 
        /// <summary>
        /// 写入text文本内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public static void WriteText(string path, string data)
        {
            //判断路径是否存在
            if (!System.IO.File.Exists(path))
            {
                throw new Exception("文件不存在");
            }
 
            //文件覆盖方式添加内容
            System.IO.StreamWriter file = new System.IO.StreamWriter(path, false);
            //保存数据到文件
            file.Write(data);
            //关闭文件
            file.Close();
            //释放对象
            file.Dispose();
        }
 
 
        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="_strPath">路径</param>
        /// <param name="_tableName">表名</param>
        /// <param name="_code">生成的代码字符串</param>
        /// <param name="fileType">生成的文件类型</param>
        /// <param name="filePrefixName">前缀</param>
        /// <param name="entityName">实体类名</param>
        /// <param name="modulelogo">模块名字</param>
        /// <returns></returns>
        public static bool Export2File(string _strPath, string _tableName, string _code, FileType fileType,
            string filePrefixName, string entityName, string modulelogo)
        {
            string fileFolderPath = _strPath + "\\" + _tableName;
 
            string fileTypeName = "";
            switch (fileType)
            {
                case FileType.Model:
                    fileTypeName = ".cs";
                    break;
                case FileType.AddModelParam:
                    entityName = "Add" + modulelogo + "Param";
                    fileTypeName = ".cs";
                    break;
                case FileType.IBLL:
                    entityName = "I" + filePrefixName + "BLL";
                    fileTypeName = ".cs";
                    break;
                case FileType.Controller:
                    entityName = entityName + "Controller";
                    fileTypeName = ".cs";
                    break;
                case FileType.JS:
                    entityName = filePrefixName;
                    fileTypeName = ".js";
                    break;
                case FileType.CSHTML_List:
                    entityName = filePrefixName;
                    fileTypeName = ".cshtml";
                    break;
                case FileType.CSHTML_Detail:
                    entityName = filePrefixName;
                    fileTypeName = "Detail.cshtml";
                    break;
                case FileType.XML:
                    entityName = filePrefixName;
                    fileTypeName = ".xml";
                    break;
                case FileType.Proc:
                    entityName = filePrefixName;
                    fileTypeName = "Proc.sql";
                    break;
                case FileType.DAL:
                    entityName = filePrefixName;
                    fileTypeName = "DAL.cs";
                    break;
                case FileType.BLL:
                    entityName = filePrefixName;
                    fileTypeName = "BLL.cs";
                    break;
                case FileType.InputModel:
                    entityName = entityName + "Input";
                    fileTypeName = ".cs";
                    fileFolderPath += "\\Dto";
                    break;
                case FileType.OutputModel:
                    entityName = entityName + "Output";
                    fileTypeName = ".cs";
                    fileFolderPath += "\\Dto";
                    break;
                case FileType.WCF_InterFace:
                    entityName = "I" + entityName + "Service";
                    fileTypeName = ".cs";
                    break;
                case FileType.WCF_InterFaceRealize:
                    entityName = entityName + "Service";
                    fileTypeName = ".cs";
                    break;
                case FileType.SQL_Insert:
                    entityName = _tableName + "InsertSQL";
                    fileTypeName = ".txt";
                    break;
                case FileType.VUE_FunConfig:
                    entityName = _tableName + "VUE方法配置";
                    fileTypeName = ".txt";
                    break;
                case FileType.VUEFile:
                    entityName = modulelogo;
                    fileTypeName = ".vue";
                    break;
            }
            if (!Directory.Exists(fileFolderPath))
            {
                Directory.CreateDirectory(fileFolderPath);
            }
            string filePath = fileFolderPath + "\\" + entityName + fileTypeName;
            using (StreamWriter outfile = new StreamWriter(filePath, false, Encoding.GetEncoding("UTF-8")))
            {
                outfile.Write(_code);
            }
 
            return true;
        }
    }
}