开发者

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

开发者 https://www.devze.com 2023-02-21 21:30 出处:网络
So i have a application that uses check boxes and radio buttons to scan a computer for virus then have each anti virus create a log of its actions. What id like to do is (based on which check boxes (t

So i have a application that uses check boxes and radio buttons to scan a computer for virus then have each anti virus create a log of its actions. What id like to do is (based on which check boxes (there's 5 total) are checked) have a message box pop up when its all done and have read each text file for a key word then read that line, for all 5 text files (if all 5 were created could be 1,2,3,4 or 5 of them). So when its all done it will just have 1 message box pop up with the info from all 5 text files 1 line each like "Panda found 5 viruses" next line "A Squared found 0 viruses" etc. then when the message box is closed, delete the text files. I know how to do this 开发者_运维问答with 1 check box and 1 text file but i don't know how to do this with multiple check boxes and multiple text files. My single file reader works like this:

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");

any help would be nice thanks. OH and C# .net 2.0 ONLY please


You can use StringWriter to append the current line and display in Message Box at last

        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());

Hope this will help you


I hope I am understanding the question right, but if I rephrase it. You want to process the 5 files and when all done show a summary of what was found?

If so why not simply do this

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());
}

And just because I don't like how you dispose (ie you don't on any exception), implement the methods like this

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!" }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消