private void DownloadAndMerge(object param) { NovelDownloadStruct novelStruct = (NovelDownloadStruct)param; List <string> links = novelStruct.links; string targetFile = novelStruct.targetFile; string dir = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMddhhmmssffff"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } else { string cmd = " del /Q \"" + dir + "\\*.*\""; string output = ""; CmdHelper.RunCmd(cmd, out output); } if (links.Count > 0) { double per = 0.95 / links.Count; for (int i = 0; i < links.Count; i++) { string eachPath = string.Empty; if (links[i].IndexOf("/") > -1) { Uri uri = new Uri(txtSource.Text); eachPath = uri.Scheme + "://" + uri.Host + links[i].Replace(txtSource.Text, "/"); } else { eachPath = txtSource.Text + Path.GetFileName(links[i]); } string htmlText = GetHtmlString(eachPath); //htmlText = DealWithHtml(htmlText); htmlText = NoHTML(htmlText); string fileName = Path.GetFileNameWithoutExtension(eachPath); string path = dir + "\\" + fileName + ".txt"; if (!filePathList.Contains(path)) { filePathList.Add(path); MergeText(htmlText, path); int progress = (int)((i + 1) * per * 100); m_context.Post(UpdateUI, progress); } } //string cmd = @"copy *.txt temp.txt"; string cmd = "copy \"" + dir + "\\*.txt\" \"" + dir + "\\temp.txt\""; string output = ""; CmdHelper.RunCmd(cmd, out output); //File.Move("temp.txt", targetFile); File.Move(dir + "\\temp.txt", targetFile); DeleteAllFile(); m_context.Post(UpdateUI, 100); MessageBox.Show("下载完成"); } }
private void btnDownload_Click(object sender, EventArgs e) { progressBar1.Value = 0; progressText.Text = "0%"; if (!string.IsNullOrEmpty(txtSource.Text)) { try { string title = string.Empty; if (txtSource.Text.LastIndexOf("/") <= -1) { txtSource.Text += "/"; } string htmlText = GetHtmlString(txtSource.Text); List <string> linkList = GetAllLinks(htmlText, ref title); txtTarget.Text = Directory.GetCurrentDirectory() + "\\" + title + ".txt"; string targetFile = title + ".txt"; //DownloadAndMerge(linkList, targetFile); NovelDownloadStruct novelStruct = new NovelDownloadStruct(); novelStruct.links = linkList; novelStruct.targetFile = targetFile; Thread thread = new Thread(DownloadAndMerge); thread.IsBackground = true; thread.Start(novelStruct); threadList.Add(thread); } catch (Exception ex) { MessageBox.Show("检查地址是否正确"); } } else { MessageBox.Show("小说目录地址不能为空"); } }