-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
272 lines (248 loc) · 9.1 KB
/
Copy pathProgram.cs
File metadata and controls
272 lines (248 loc) · 9.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Text;
using WFormMarkDown.Entitys;
using WFormMarkDown.Common;
namespace WFormMarkDown
{
static class Program
{
/// <summary>
/// 程序所在目录
/// </summary>
private static string exeDir;
/// <summary>
/// 程序创建的数据主目录
/// </summary>
private static string baseDir;
/// <summary>
/// 配置文件路径
/// </summary>
private static string configDir;
/// <summary>
/// 其他配置数据所在目录
/// </summary>
private static string dataDir;
public static string GetDataDir()
{
return dataDir;
}
/// <summary>
/// 博客文件所在目录
/// </summary>
private static string blogDir;
public static string GetBlogDir()
{
return blogDir;
}
/// <summary>
/// MarkDown文件所在目录
/// </summary>
private static string markDownDir;
public static string GetMarkDownDir()
{
return markDownDir;
}
/// <summary>
/// 程序的主配置文件
/// </summary>
private static Entitys.ConfigEntity Config;
public static Entitys.ConfigEntity GetConfig()
{
return Program.Config;
}
/// <summary>
/// 是否查看
/// </summary>
private static bool IsRunInLocal = false;
public static bool GetIsRunInLocal()
{
return IsRunInLocal;
}
public static bool SetIsRunInLocal(bool state)
{
IsRunInLocal = state;
return IsRunInLocal;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
exeDir = Environment.CurrentDirectory;
baseDir = exeDir + "\\HexoData";
configDir = baseDir + "\\config.json";
dataDir = baseDir + "\\Data";
blogDir = baseDir + "\\Blog";
markDownDir = baseDir + "\\MarkDown";
if (!InitProgram())
{
return;
}
LoadConfig();
Application.Run(new Form1());
}
/// <summary>
/// 初始化文件目录
/// </summary>
/// <returns>初始化程序</returns>
private static bool InitProgram()
{
try
{
if (!Directory.Exists(baseDir))
{
DialogResult re = MessageBox.Show("是否进行数据初始化?", "数据初始化", MessageBoxButtons.YesNo);
if (re == DialogResult.Yes)
{
// 创建
Directory.CreateDirectory(baseDir);
}
else {
return false;
}
}
if (!File.Exists(configDir))
{
//如果不存在 则从嵌入资源内读取 BlockSet.xml
Assembly asm = Assembly.GetExecutingAssembly();//读取嵌入式资源
Stream sm = asm.GetManifestResourceStream("WFormMarkDown.DLL.HexoData.config.json");
StreamReader sr = new StreamReader(sm);
string configContent = sr.ReadToEnd();
sr.Close();
Entitys.ConfigEntity configEntity = Newtonsoft.Json.JsonConvert.DeserializeObject<Entitys.ConfigEntity>(configContent);
configEntity.BlogDirectory = blogDir;
configEntity.CurrentDirectory = baseDir;
configContent = Newtonsoft.Json.JsonConvert.SerializeObject(configEntity);
using (StreamWriter sw = File.CreateText(configDir))
{
sw.Write(configContent);
sw.Close();
}
}
if (!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
}
if (!Directory.Exists(markDownDir))
{
Directory.CreateDirectory(markDownDir);
}
string readmepath = Path.Combine(markDownDir + "\\README.MD");
if (!File.Exists(readmepath))
{
Entitys.BlogHead blogHead = new Entitys.BlogHead();
blogHead.type = "";
blogHead.tags = new List<string>();
blogHead.photos = new List<string>();
blogHead.description = "";
blogHead.date = DateTime.Now;
blogHead.title = "Hello World";
StringBuilder headStr = new StringBuilder();
headStr.AppendLine("---StartBlogHead");
headStr.AppendLine(JsonPrase.PraseToJson(Newtonsoft.Json.JsonConvert.SerializeObject(blogHead)));
headStr.AppendLine("---EndBlogHead");
headStr.AppendLine("");
headStr.AppendLine(BlogHeadHelper.GetHelloWorld());
StreamWriter sw = File.CreateText(readmepath);
sw.Write(headStr.ToString());
sw.Close();
}
if (!Directory.Exists(blogDir))
{
Directory.CreateDirectory(blogDir);
}
string stylesDir = Path.Combine(blogDir, "Styles");
if (!Directory.Exists(stylesDir))
Directory.CreateDirectory(stylesDir);
string scriptsDir = Path.Combine(blogDir, "Scripts");
if (!Directory.Exists(scriptsDir))
Directory.CreateDirectory(scriptsDir);
string baseStyle = Path.Combine(blogDir, "Styles", "style.css");
if (!File.Exists(baseStyle))
{
//如果不存在 则从嵌入资源内读取 BlockSet.xml
Assembly asm = Assembly.GetExecutingAssembly();//读取嵌入式资源
Stream sm = asm.GetManifestResourceStream("WFormMarkDown.DLL.web.style.css");
StreamReader sr = new StreamReader(sm);
string baseStyleContent = sr.ReadToEnd();
sr.Close();
using (StreamWriter sw = File.CreateText(baseStyle))
{
sw.Write(baseStyleContent);
sw.Close();
}
}
string markdownStyle = Path.Combine(blogDir, "Styles", "markdownstyle.css");
if (!File.Exists(markdownStyle))
{
//如果不存在 则从嵌入资源内读取 BlockSet.xml
Assembly asm = Assembly.GetExecutingAssembly();//读取嵌入式资源
Stream sm = asm.GetManifestResourceStream("WFormMarkDown.DLL.web.markdownstyle.css");
StreamReader sr = new StreamReader(sm);
string markdownStyleContent = sr.ReadToEnd();
sr.Close();
using (StreamWriter sw = File.CreateText(markdownStyle))
{
sw.Write(markdownStyleContent);
sw.Close();
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
/// <summary>
/// 加载配置文件
/// </summary>
/// <returns></returns>
private static bool LoadConfig()
{
try
{
string config = File.ReadAllText(configDir, System.Text.Encoding.UTF8);
Config = Newtonsoft.Json.JsonConvert.DeserializeObject<Entitys.ConfigEntity>(config);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
/// <summary>
/// 保存配置文件
/// </summary>
/// <returns></returns>
public static bool SaveConfig()
{
try
{
string config = Newtonsoft.Json.JsonConvert.SerializeObject(GetConfig());
using (StreamWriter sw = new StreamWriter(configDir))
{
sw.Write(config);
sw.Close();
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
}