繁体   English   中英

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

[英]How can i read and display info from multiple text files in 1 message box C#?

所以我有一个应用程序,使用复选框和单选按钮扫描计算机中的病毒,然后让每个反病毒创建其操作的日志。 我喜欢做的是(基于哪个复选框(总共有5个)被检查)当它全部完成时弹出一个消息框,并读取每个文本文件中的关键字然后读取该行,对于所有5个文本文件(如果创建全部5个可能是1,2,3,4或5个)。 因此,当它全部完成时,它将只弹出一个消息框,其中包含来自所有5个文本文件的信息,每行1个,如“Panda发现5个病毒”,下一行“A Squared发现0个病毒”等,然后当消息框关闭时,删除文本文件。 我知道如何使用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");

任何帮助都会很好,谢谢。 只有OH和C#.net 2.0

您可以使用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());

希望这个能对您有所帮助

我希望我能正确理解这个问题,但如果我改写它。 您想要处理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!" }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM