![](/img/trans.png)
[英]i want to display text from textfile in text box . how can i do this .. in 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.