//读现有日记文件夹 public IActionResult ReadDiary() { Stopwatch sw = new Stopwatch(); sw.Start(); string dirpath = @"C:\temp\diary\"; string[] ss = System.IO.Directory.GetFiles(dirpath); StringBuilder sb = new StringBuilder(); foreach (var item in ss) { try { string title = new System.IO.FileInfo(item).Name; string body = System.IO.File.ReadAllText(item); if (diarydal.CalcCount($"title='{title}'") == 0) { diarydal.Add(new Model.Diary() { title = title, body = body, createtime = DateTime.Now }); } } catch (Exception ex) { sb.Append($"插入【{item}】出错:【{ex.Message}】 "); continue; } } sw.Stop(); sb.Append("运行时间:" + sw.ElapsedMilliseconds + "毫秒"); return(Content(sb.ToString())); }
public ActionResult Add(Model.Diary m) { try { if (m.id == 0) { m.createtime = DateTime.Now; dal.Add(m); return(Json(new { code = 0, msg = "新增成功!" })); } else { dal.Update(m); return(Json(new { code = 0, msg = "编辑成功!" })); } } catch (Exception ex) { return(Json(new { code = 1, msg = $"出错:{ex.Message}" })); } }