private void btn_Create_Click(object sender, EventArgs e) { if (Directory.Exists(baseDir)) { if (string.IsNullOrWhiteSpace(tBox_FileName.Text)) { MessageBox.Show("请输入文件名"); return; } string fileName = tBox_FileName.Text.Trim().Replace(' ', '_'); string filepath = Path.Combine(baseDir, fileName); if (filepath.Substring(filepath.Length - 2).ToUpper() != ".MD") { filepath += ".md"; } if (File.Exists(filepath)) { MessageBox.Show("文件名已经存在!"); return; } StreamWriter sw = File.CreateText(filepath); Entitys.BlogHead blogHead = new Entitys.BlogHead(); blogHead.type = ""; blogHead.tags = new List <string>(); blogHead.title = fileName; blogHead.photos = new List <string>(); blogHead.description = ""; blogHead.date = DateTime.Now; StringBuilder headStr = new StringBuilder(); headStr.AppendLine("---StartBlogHead"); headStr.AppendLine(JsonPrase.PraseToJson(Newtonsoft.Json.JsonConvert.SerializeObject(blogHead))); //headStr.AppendLine("{"); //headStr.AppendLine(string.Format(" \"title\": \"{0}\",", fileName)); //headStr.AppendLine(" \"date\": \"0001-01-01T00:00:00\","); //headStr.AppendLine(" \"type\": \"\","); //headStr.AppendLine(" \"tags\": [],"); //headStr.AppendLine(" \"photos\": [],"); //headStr.AppendLine(" \"description\": \"\""); //headStr.AppendLine("}"); headStr.AppendLine("---EndBlogHead"); headStr.AppendLine(""); headStr.AppendLine("##Hello World!"); sw.Write(headStr.ToString()); sw.Close(); DelLeftTreeEvent(); this.Close(); } else { MessageBox.Show("请重新选择文件目录!"); } }
/// <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); } }