Beispiel #1
0
        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);//返回总行数
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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界面一次性绘制。
        }
Beispiel #4
0
        /// <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);//返回总行数
        }
Beispiel #5
0
        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, "数据加载完成");
            }
        }