如何在C#中读取并显示多个文本文件的信息在一个消息框中?

3

我有一个应用程序,使用复选框和单选按钮来扫描计算机上的病毒,然后让每个防病毒软件创建其操作日志。我的想法是(基于选择的哪些复选框(总共有5个)),当所有操作完成并读取了每个文本文件中的关键字时,弹出一个消息框,然后读取每个文本文件的一行,对于所有5个文本文件(如果创建了全部5个,则可以是1、2、3、4或5个)。因此,当所有操作完成后,它将只弹出1个消息框,并显示来自所有5个文本文件的信息,每个文件显示1行,例如“Panda发现5个病毒”下一行“ A Squared未发现病毒”等等。然后,当关闭消息框时,删除文本文件。我知道如何使用1个复选框和1个文本文件来完成此操作,但我不知道如何使用多个复选框和多个文本文件。我的单个文件读取器的工作原理如下:

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("C:\\Panda.txt");
while ((line = file.ReadLine()) != null)
{
    if (line.Contains("Number of files infected"))
    {
        MessageBox.Show("Panda " + line);
    }
}
file.Dispose();
System.IO.File.Delete("C:\\Panda.txt");

希望能得到任何帮助,谢谢。哦,还有只需要C# .net 2.0。


使用行MessageBox.Show(writer);时,我收到了以下错误信息:“System.Windows.Forms.MessageBox.show(string)的最佳重载方法存在一些无效参数”和“Argument 1: cannot convert from 'System.IO.StringWriter' to 'string'”。 - NightsEVil
几乎完美地工作,只是我需要某种错误处理方式,因为并不总是会选中选项1、2、3、4和5,有时可能只有1、2和5,或者只有3。目前它只查找两个文本文件,如果第一个找不到就会出错。 - NightsEVil
2个回答

1

你可以使用 StringWriter 来追加当前行并最终在消息框中显示

        string FirstPanda = "C:\\FirstPanda.txt";
        string SecondPanda = "C:\\SecondPanda.txt";


        StringWriter writer = new StringWriter(); // System.IO;
        System.IO.StreamReader file;

        if (firstCheckBox.IsChecked)  
        {
            if (File.Exists(FirstPanda))
            {
                file = new System.IO.StreamReader(FirstPanda);
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("Number of files infected"))
                    {

                        writer.WriteLine("Panda " + line);
                    }
                }
                file.Close();
                System.IO.File.Delete(FirstPanda);
            }
        }

        if (secondCheckBox.IsChecked)
        {
            if (File.Exists(SecondPanda))
            {
                file = new StreamReader(SecondPanda);

                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("Number of files infected"))
                    {

                        writer.WriteLine("Panda " + line);
                    }
                }
                file.Close();

                System.IO.File.Delete(SecondPanda);
            }
        }
        MessageBox.Show(writer.ToString());

希望这可以帮助你。

几乎完美地工作,只是我需要某种错误处理方式,因为并不总是会选中选项1、2、3、4和5,有时可能只有1、2和5,或者只有3。目前它只查找两个文本文件,如果第一个找不到就会出错。 - NightsEVil
如何使其在读取文件之前检查文件是否存在?因为如果说Panda.txt不存在,但NextPand.txt存在...它会抛出一个错误。 - NightsEVil
使用 File.Exists(path) 来检查它们是否存在。 - Hukam
好的,是的,File.Exists(路径)似乎解决了问题,谢谢。但是,现在我该如何修改'if(line.Contains(“”))'行以读取一个文件的多行内容?因为其中一个txt文件需要读取4行,所以它需要查找单词“Found”,然后读取接下来的5行(5行是因为找到后的第一行是空格)。 - NightsEVil

0

我希望我正确理解了这个问题,如果我重新陈述一下。您想处理这5个文件,在所有文件处理完成后显示发现的摘要吗?

如果是这样,为什么不这样做呢?

public void ProcessFiles()
{
  var resultText = new StringBuilder();

  if (PandaCheckBox.Checked)
  {
    var result = DoPandaProcessing(); // Read file and do whatever here
    resultText.Append(result.ResultMessage);
  }

  if (Process2CheckBox.Checked)
  {
    var result = DoProcess2Processing();
    if (resultText.Length > 0)
    {
      resultText.Append(Environment.NewLine);
    }
    resultText.Append(result.ResultMessage);
  }

  MessageBox.Show(resultText.ToString());
}

仅仅因为我不喜欢你的处理方式(即你没有在任何异常情况下进行处理),所以请按照以下方式实现这些方法

public ProcessResult DoPandaProcessing()
{
  using (var file = File.OpenText("filename.txt"))
  {
    while ((line = file.ReadLine()) != null)
    {
       if (line.Contains("Number of files infected"))
       {
          return new ProcessResult { Status = Status.Success, ResultMessage = "Panda " + line };
       }
    }

    return new ProcessResult { Status = Status.Failure, ResultMessage = "Panda: No result found!" }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接