private void btnCombine_Click(object sender, EventArgs e) { if (txtInFolder.Text == String.Empty) { return; } if (!Directory.Exists(txtOutFolder.Text)) { Directory.CreateDirectory(txtOutFolder.Text); } List <string> fileLists = new List <string>(); for (int i = dataGridView1.SelectedRows.Count - 1; i > -1; i--) { fileLists.Add(txtInFolder.Text + dataGridView1.SelectedRows[i].Cells[2].Value); dataGridView1.SelectedRows[i].Cells[0].Style.BackColor = Color.Green; dataGridView1.SelectedRows[i].Cells[0].Value = "已合并" + (dataGridView1.SelectedRows.Count - i); } string path = txtOutFolder.Text + "合并数据" + dataGridView1.SelectedRows[dataGridView1.SelectedRows.Count - 1].Cells[2].Value + "-" + dataGridView1.SelectedRows[0].Cells[2].Value + "(" + lblSelectedCount.Text + ")" + ".mca"; Mca.CombineMCA(fileLists, path); MessageBox.Show(" 合并完成!\n\n成功合并文件数:" + dataGridView1.SelectedRows.Count + "\n数据条数:" + lblSelectedCount.Text, "完成提示框", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static int checkMca(List <string> fileNames) { int count = 0; //统计文件数量 int lines = 0; //统计行数量 int firstLenght = 0; //第一行的长度 string title = null; //表头参数 bool isTitleError = false; //表头错误 string errorInfo = ""; //出错文件名 bool isLineError = false; //行错误 //=====遍历传入的列表===== foreach (string fileName in fileNames) { count++; using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) { string current = null; current = reader.ReadLine(); //检查表头 if (count == 1) { title = current;//保存表头 } else { if (current != title)//校验表头 { isTitleError = true; errorInfo = "表头错误\n" + Mca.GetFileName(fileName); lines = -1; break; } } //检查内容 while ((current = reader.ReadLine()) != null) { lines++; if (lines == 1) { firstLenght = current.Length;//保存内容中第一行长度 } else { if (current.Length != firstLenght)//校验行 { isLineError = true; errorInfo = "行数量错误\n" + Mca.GetFileName(fileName); lines = -1; break; } } } } } if (isTitleError == true || isLineError == true) { MessageBox.Show(errorInfo, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(lines);//返回总行数 }
private DataTable GetFileTable(string path) { List <string> fileList = new List <string>(); fileList.Clear(); fileList = Mca.GetMcaList(path); DataTable dataTable = new DataTable(); dataTable.Columns.Add("状态", Type.GetType("System.String")); dataTable.Columns.Add("数量", Type.GetType("System.String")); dataTable.Columns.Add("文件名", Type.GetType("System.String")); foreach (string file in fileList) { //int index = this.dataGridView1.Rows.Add(); //this.dataGridView1.Rows[index].Cells[0].Value = "未合并"; //this.dataGridView1.Rows[index].Cells[0].Style.BackColor = Color.Yellow; //this.dataGridView1.Rows[index].Cells[1].Value = mca.GetFileLinesCount(file) - 2; //this.dataGridView1.Rows[index].Cells[2].Value = mca.GetFileName(file); DataRow row = dataTable.NewRow(); row[0] = "未合并"; row[1] = Mca.GetFileLineCount(file) - 2; row[2] = Mca.GetFileName(file); dataTable.Rows.Add(row); } return(dataTable); }
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("诶呀!出问题了"); //} }
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); }
private void AddListViewItems(string path) { lvList.Items.Clear(); if (path.Trim() == "") { return; } List <string> fileLists = Mca.GetMcaList(path); int count = 0; //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 this.lvList.BeginUpdate(); foreach (string file in fileLists) { count++; ListViewItem lvi = new ListViewItem(); lvi.Text = count.ToString(); lvi.SubItems.Add(Mca.GetFileName(file)); lvi.SubItems.Add((Mca.GetFileLineCount(file) - 2).ToString()); lvList.Items.Add(lvi); } this.lvList.EndUpdate();//结束数据处理,UI界面一次性绘制。 }
/// <summary> /// 合并mca,校验每行长度 /// </summary> /// <param name="fileNames"></param> /// <param name="outPath"></param> /// <returns></returns> public static int CombineMCAByCheck(List <string> fileNames, string outFile) { int count = 0; //统计文件数量 int lines = 0; //统计行数量 int firstLenght = 0; //第一行的长度 string title = null; //表头参数 bool isTitleError = false; //表头错误 string errorTitle = ""; //出错文件名 bool isLineError = false; //行错误 //合并前删除缓存 if (File.Exists(outFile)) { File.Delete(outFile); } StreamWriter writer = new StreamWriter(outFile, true, Encoding.Default); foreach (string fileName in fileNames) { count++; StreamReader reader = new StreamReader(fileName, Encoding.Default); string current = null; current = reader.ReadLine();//表头 if (count == 1) { writer.WriteLine(current);//输出第一个表头 title = current; } else { if (current != title)//校验表头 { isTitleError = true; errorTitle = Mca.GetFileName(fileName); break; } } //内容 while ((current = reader.ReadLine()) != null) { lines++; if (lines == 1) { firstLenght = current.Length; } else { if (current.Length != firstLenght)//校验行 { isLineError = true; errorTitle = Mca.GetFileName(fileName); break; } } writer.WriteLine(current); } if (isLineError == true) { break; } reader.Close(); } writer.Close(); if (isTitleError == true) { System.Windows.Forms.MessageBox.Show("表头不一致" + errorTitle, "错误提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); lines = -1; } if (isLineError == true) { System.Windows.Forms.MessageBox.Show("行长度不一致" + errorTitle, "错误提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); lines = -1; } return(lines);//返回总行数 }
private void AddListViewItems(object obj) { //拆箱 parameters p = (parameters)obj; string path = p.one; string filePath = p.two; if (filePath.Trim() == "") { return; } if (path.Trim() == "") { return; } //开始加载数据 this.Invoke(myShowStatus, "数据加载中"); string[] files = File.ReadAllLines(filePath, Encoding.Default); this.Invoke(myProgressInit, files.Length * 2); //显示数据列表 int fileCount = 0; int lineCount = 0; int listNum = 0;//文件列表序号 List <string> existList = new List <string>(); List <ThreeParameters> list = new List <ThreeParameters>(); //加载数据列表,显示 foreach (string file in files) { string fullName = Path.Combine(path, file); listNum++; ThreeParameters threePara = new ThreeParameters(); threePara.one = listNum.ToString(); if (File.Exists(fullName)) //文件存在检查 { fileCount++; //文件计数 existList.Add(fullName); threePara.two = Mca.GetFileName(fullName); int lines = Mca.GetFileLineCount(fullName) - 2; lineCount += lines; threePara.three = lines.ToString(); } else { threePara.two = ""; threePara.three = ""; } list.Add(threePara); this.Invoke(myPerformStep); } //校验数据 this.Invoke(myShowStatus, "数据校验中"); int counts = Mca.checkMca(existList); if (counts == -1)//校验出错 { this.Invoke(myProgressInit, 0); this.Invoke(myShowStatus, "数据校验出错"); } else { for (int i = 0; i < files.Length; i++) { this.Invoke(myPerformStep); } parameters pLbl = new parameters(); pLbl.one = fileCount.ToString(); pLbl.two = lineCount.ToString(); this.Invoke(myShowCount, pLbl); // 显示数据列表 this.Invoke(myAddListView, list); this.Invoke(myShowStatus, "数据加载完成"); } }