public static async void DownWork(string dirpath) { ImageDown down = new ImageDown(dirpath); string[] files = Directory.GetFiles(dirpath); foreach (var item in exclude.File_Exclude) { files = files.Where(x => x.ToLower() != (Path.Combine(dirpath, item)).ToLower()).ToArray(); } if (files.Length != 0) { if (!Directory.Exists(Path.Combine(dirpath, dirName))) { Directory.CreateDirectory(Path.Combine(dirpath, dirName)); } } foreach (var item in files) { if (isDown) { await ReadMd(item); } else { await ReadMd(item, down); } } }
public static async Task ReadMd(string mdName, ImageDown down = null) { Console.WriteLine($"处理文件:{mdName}"); StreamReader streamReader = File.OpenText(mdName); string lines = streamReader.ReadToEnd(); string lines2 = lines; streamReader.Close(); streamReader.Dispose(); if (lines.Length < 10) { return; } bool yes = false; bool yesL = false; int start = 0; int lineStart = 0; int end = 0; for (int i = 0; i < lines.Length - 1; i++) { if (lines[i] == '!' && lines[i + 1] == '[') { start = i; yes = true; } if (yes == true && start != 0 && lines[i] == '(') { lineStart = i; yesL = true; } if (yes == true && yesL == true && lines[i] == ')') { end = i; yes = false; yesL = false; string url = lines.Substring(lineStart + 1, end - lineStart - 1); Console.WriteLine("处理图片地址:" + url); // 图片名称 string imageName = url.Substring(url.LastIndexOf("/") + 1); string filePath = Path.Combine(Path.Combine(Path.GetDirectoryName(mdName), dirName), imageName); if (isDown == true) { down.GetImgAsync(url, filePath); } string rpStr = "./" + dirName + "/" + imageName; lines2 = lines2.Replace(url, rpStr); start = 0; lineStart = 0; end = 0; } } using (var fileStream = new FileStream(mdName, FileMode.Create)) { await fileStream.WriteAsync(Encoding.Default.GetBytes(lines2)); fileStream.Flush(); fileStream.Close(); } }