/// <summary> /// 生成补卡数据:方案1-1,先打开补卡卡号,逐个去查找 /// </summary> /// <param name="mypath">原始数据路径,补卡卡号路径</param> private void FindData(object mypath) { myPath objmypath = (myPath)mypath; string sourcePath = objmypath.pathone; string findSource = objmypath.pathtwo; StreamReader reader = new StreamReader(findSource, Encoding.Default); string sourceName = Path.GetFileName(findSource); string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string outFile = Path.Combine(desktop, "BK_" + sourceName); StreamWriter writer = new StreamWriter(outFile, false, Encoding.Default); string key = null; string value = ""; int failCount = 0; int successCount = 0; while ((key = reader.ReadLine()) != null) { value = findLine(sourcePath, key); if (value != null) { writer.WriteLine(value); successCount++; } else { failCount++; } } writer.Close(); reader.Close(); MessageBox.Show("补卡数据生成完成!\n成功:" + successCount + "\n失败:" + failCount); }
string combineMcaPath = "";//全局变量保存临时合并mca的路径,所有线程执行完毕之后删除。 private void btnCereate_Click(object sender, EventArgs e) { if (txtFindData.Text == "") { return; } if (txtSourceData.Text == "") { return; } //保存路径 //string combineMca = "Combine" + Path.GetFileNameWithoutExtension(txtSourceData.Text) + ".tmp"; //string filePath = Path.GetDirectoryName(txtSourceData.Text); combineMcaPath = Path.Combine(txtSourceData.Text, "Combine.tmp"); //获取mca文件列表 List <string> sourceFiles = Mca.GetMcaList(txtSourceData.Text); Mca.CombineMCA(sourceFiles, combineMcaPath); //获取补卡卡号文件列表 FileHandle fileHandle = new FileHandle(txtFindData.Text); List <string> findFiles = fileHandle.GetFileList("*.*"); Threadcounts = findFiles.Count;//补卡文件总和 //同时开启多线程工作 foreach (string files in findFiles) { myPath mypath = new myPath(); mypath.pathone = combineMcaPath; //txtSourceData.Text; mypath.pathtwo = files; //txtFindData.Text; Thread t = new Thread(FindDataAll); t.Start(mypath); } //string path=TrimMca(txtFindData.Text); //MessageBox.Show(path); }
/// <summary> /// 通过输入原始文件和补卡卡号输出补卡数据:方案二 /// </summary> /// <param name="mypath">原始数据路径,补卡卡号路径</param> private void FindDataAll(object mypath) { //解析路径 myPath objmypath = (myPath)mypath; string sourcePath = objmypath.pathone; string findPath = objmypath.pathtwo; //判断文件是否存在 if (!File.Exists(sourcePath)) { return; } if (!File.Exists(findPath)) { return; } //读取补卡文件 string sortPath = SortMca(findPath); //排序,从小到大 string trimPath = TrimMca(sortPath); //去重,去空白行 StreamReader readerFind = new StreamReader(trimPath, Encoding.Default); //读取原始文件 StreamReader readerSource = new StreamReader(sourcePath, Encoding.Default); //保存补卡数据 string findName = Path.GetFileNameWithoutExtension(findPath); //string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string saveDir = Path.GetDirectoryName(findPath); string outFile = Path.Combine(saveDir, "BK_" + findName + ".mca"); StreamWriter writer = null; try { //定义必要的变量 string current = null; string keyTitle = ""; string key = readerFind.ReadLine();//先读取补卡卡号的表头 if (key != null && key != "") { keyTitle = key; } else { MessageBox.Show("文件:" + Path.GetFileName(findPath) + "\n表头参数不对!不能为空值"); return; } //校验原始数据中表头参数匹配 current = readerSource.ReadLine(); if (!((current.IndexOf(keyTitle)) > -1)) { MessageBox.Show("文件:" + Path.GetFileName(findPath) + "\n原始数据中没有表头参数:\n" + keyTitle); return; } //输出表头 writer = new StreamWriter(outFile, false, Encoding.Default); writer.WriteLine(current); //确定关键字在数据行中以逗号分隔的索引 int index = -1; string[] keys = current.Split(','); for (int i = 0; i < keys.Length; i++) { if (keyTitle == keys[i]) { index = i; } } //创建原始数据,以keytitle为键,当前行为值的,字典数据 Dictionary <string, string> dict = new Dictionary <string, string>(); while ((current = readerSource.ReadLine()) != null) { dict.Add(current.Split(',')[index], current);//可能会下标越界 //Debug.WriteLine(current.Split(',')[index], current); //File.AppendAllText("log.txt", current+" | "+current.Split(',')[index]+"\r\n"); } //通过关键字在原始数组字典中查找对应的值 int failCount = 0; int successCount = 0; while ((key = readerFind.ReadLine()) != null) { //value = findLine(sourcePath, key); if (dict.ContainsKey(key)) { writer.WriteLine(dict[key]); successCount++; } else { failCount++; } } if (failCount > 0) { MessageBox.Show("文件:" + findName + "\n补卡数据生成完成!\n成功:" + successCount + "\n失败:" + failCount, "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Error); } //else //{ // MessageBox.Show("补卡数据生成完成!\n成功:" + successCount + "\n失败:" + failCount, "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Information); //} } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { readerSource.Close(); if (writer != null) { writer.Close(); } readerFind.Close(); if (File.Exists(sortPath)) { File.Delete(sortPath); //删除排序临时文件 } if (File.Exists(trimPath)) { File.Delete(trimPath); //删除去重临时文件 } Threadcount++; if (Threadcount == Threadcounts)//所有线程执行完毕 { if (File.Exists(combineMcaPath)) { File.Delete(combineMcaPath); //删除合并临时文件 } MessageBox.Show("补卡数据生成完成!", "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }