我正在嘗試讀取文本文件並提取其中的所有電子郵件地址。得到這個工作具有以下功能:C#在函數執行時填充進度條
我的C#功能:
public void extractMails(string filePath)
{
List<string> mailAddressList = new List<string>();
string data = File.ReadAllText(filePath);
Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection emailMatches = emailRegex.Matches(data);
StringBuilder sb = new StringBuilder();
foreach (Match emailMatch in emailMatches)
{
sb.AppendLine(emailMatch.Value);
}
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
string dirPath = Path.GetDirectoryName(exePath);
File.WriteAllText(dirPath + "extractedEmails.txt", sb.ToString());
}
現在我添加了一個進度條,因爲加載的文本文件可以是巨大的。當函數執行時,我怎麼能填充進度條,最後進度條會填充到100%?
我將不勝感激任何形式的幫助。
假設的WinForms,用BackgroundWorker的 –
你需要用'ReadLines'更換'ReadAllText'到逐行讀取文件中的行這對於大文件的最佳實踐,並使用async-等待模式同步執行長時間運行的代碼。 – user3185569
是它的一個winform應用程序 – d45ndx