コード例 #1
0
ファイル: Form1.cs プロジェクト: joeytall/Terminator2.5
        private void findCorruptedFiles_Click(object sender, EventArgs e)
        {
            int           fileColumnIndex = Grid.Columns["File Name"].Index;
            List <string> targetFiles     = SystemMessage.getProblemFiles();
            List <string> problemFiles    = new List <string>();
            List <string> corruptedFiles  = new List <string>();
            Dictionary <string, List <string> > msgDict = new Dictionary <string, List <string> >();

            foreach (DataGridViewRow row in Grid.Rows)
            {
                bool   corrupted = false;
                string fileName  = row.Cells[fileColumnIndex].Value.ToString();
                string filePath  = txtPath.Text + "\\" + fileName;
                if (!fileName.Contains("\\"))
                {
                    fileName = Regex.Split(txtPath.Text, @"\\").Last() + "/" + fileName;
                }
                FileName = fileName.Replace("\\", "/");
                string dbFileName = FileName.Replace(".cs", "").ToLower();

                if (!targetFiles.Contains(dbFileName))
                {
                    continue;
                }

                List <string> allLines     = File.ReadAllLines(filePath).ToList();
                List <string> linesWithMsg = allLines.Where(line => line.Contains("m_msg")).ToList();
                if (linesWithMsg.Count == 0)
                {
                    continue;
                }

                List <string> inFileMsgs = new List <string>();
                foreach (string line in linesWithMsg)
                {
                    MatchCollection matches = Regex.Matches(line, @"T\d+");
                    foreach (Match match in matches)
                    {
                        if (!inFileMsgs.Contains(match.ToString()))
                        {
                            inFileMsgs.Add(match.ToString());
                        }
                        else
                        {
                            console.AppendText(match.ToString() + " duplicated in " + FileName + ". \r\n");
                            corrupted = true;
                        }
                    }
                }
                if (!msgDict.ContainsKey(dbFileName))
                {
                    msgDict.Add(dbFileName, inFileMsgs);
                }
                else
                {
                    List <string> msgsInCurrentFile = msgDict[dbFileName];
                    foreach (string msg in inFileMsgs)
                    {
                        if (!msgsInCurrentFile.Contains(msg))
                        {
                            msgsInCurrentFile.Add(msg);
                        }
                        else
                        {
                            console.AppendText(msg + " cross file duplicated in " + FileName + ". \r\n");
                            if (!problemFiles.Contains(FileName))
                            {
                                problemFiles.Add(FileName);
                            }
                        }
                    }
                }

                if (corrupted)
                {
                    corruptedFiles.Add(FileName);
                }
            }
            console.AppendText("\r\nDone\r\n");
            console.AppendText(corruptedFiles.Count + " files corrupted. \r\n");
            console.AppendText(String.Join("\r\n", corruptedFiles).ToString() + "\r\n\r\n");
            console.AppendText(problemFiles.Count + " files cross file duplicated. \r\n");
            console.AppendText(String.Join("\r\n", problemFiles).ToString() + "\r\n\r\n");
            console.ScrollToCaret();
        }