private void btnCombine_Click(object sender, EventArgs e) { //检查输入文件路径 if (txtFolderPath.Text.Trim().Length == 0) { MessageBox.Show("未选择数据文件夹", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (lvList.Items.Count <= 0) { MessageBox.Show("未选择要合并的文件", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //从文件列表中获取文件子列表 List <string> files = new List <string>(); for (int i = 0; i < lvList.Items.Count; i++) { ListViewItem lvi = lvList.Items[i]; string file = lvi.SubItems[1].Text; files.Add(file); } files.Add(""); //作为结尾标志位 //string outPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); listpath p = new listpath(); p.list = files; p.path = txtFolderPath.Text; Thread t = new Thread(combineThread); t.Start(p); }
private void combineThread(object obj) { //拆箱 listpath p = (listpath)obj; List <string> list = p.list; string path = p.path; this.Invoke(myProgressInit, list.Count); this.Invoke(myShowStatus, "合并中..."); //建立合并文件夹,存在则删除重建 string saveFolder = Path.Combine(path, "合并文件"); if (Directory.Exists(saveFolder)) { Directory.Delete(saveFolder, true); } Directory.CreateDirectory(saveFolder); //开始合并 List <string> files = new List <string>(); int combineNum = 0; //文件名计数 int counts = 0; //总行数,最后比对 for (int i = 0; i < list.Count; i++) { if (list[i] != "") { string filePath = Path.Combine(path, list[i]); files.Add(filePath); } if (list[i] == "" && files.Count > 0) { combineNum++; string outPath = Path.Combine(saveFolder, combineNum.ToString() + ".mca"); int count = Mca.CombineMCA(files, outPath); counts += count; files.Clear(); } this.Invoke(myPerformStep); } this.Invoke(myShowStatus, "合并完成"); MessageBox.Show("合并完成!\n文件数:" + combineNum + "\n数据:" + counts, "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //catch //{ // MessageBox.Show("诶呀!出问题了"); //} }