将大文本文件加载到richtextbox c# [重复]
Posted
技术标签:
【中文标题】将大文本文件加载到richtextbox c# [重复]【英文标题】:loading large text file into richtextbox c# [duplicate] 【发布时间】:2021-09-04 12:39:53 【问题描述】:我有一个大文本文件,例如 39000 行及以上,我想将其加载到 Richtextbox 中,使用如下正常方式:
Richtextbox1.Text=File.ReadAllLines(file);
或
Richtextbox1.LoadFile(...);
即使我使用 BackgroundWorker,它也需要很长时间并冻结 UI,所以我决定将文件拆分为多个部分,每个部分为 1000 行并将它们附加到 Richtextbox,我的意思是我将 1000 行从文件读入字符串然后附加它到richtextbox,这是我的代码:
private void Worker_DoWork(object sender, DoWorkEventArgs e)
StreamReader reader = null;
try
//
int ProgressPercentage = 0;
//enable stop Correct button
BTN_Send_Ref.Invoke((MethodInvoker)(() =>
BTN_Send_Ref.Enabled = true;
));
string[] Lines = File.ReadAllLines(InputTextFile);
//MessageBox.Show("Lines : " + Lines.Length);
int i= 0;
int j = 0;
string LinesCollection = "";
while (true)
if (worker.CancellationPending)
if (reader != null)
reader.Close();
reader = null;
e.Cancel = true;
//cancel backgroundworker
return;
LinesCollection = "";
if (i > Lines.Length ||
j >= Lines.Length)
break;
i += 1000;
if (i >= Lines.Length)
i = Lines.Length ;
while ( j < i)
if (worker.CancellationPending)
if (reader != null)
reader.Close();
reader = null;
e.Cancel = true;
//cancel backgroundworker
return;
if (j < Lines.Length)
if (LinesCollection == "")
LinesCollection = Lines[j];
else
LinesCollection += Environment.NewLine + Lines[j];
ProgressPercentage = (int)Math.Round((((double)j) / Lines.Length) * 100);
if (ProgressPercentage < 100)
//report progress
worker.ReportProgress(ProgressPercentage);
ProgressPercentage++;
j++;
RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
RichTXT_OutText_Ref.AppendText(LinesCollection +Environment.NewLine);
));
LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
LBL_CountOfChars_Ref.Update();
));
LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
LBL_CountOfLines_Ref.Update();
));
/*int Tmp_Count = 0;
if (RichTXT_OutText_Ref.InvokeRequired)
RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
RichTXT_OutText_Ref.LoadFile(InputTextFile, RichTextBoxStreamType.PlainText);
));
else
RichTXT_OutText_Ref.LoadFile(InputTextFile, RichTextBoxStreamType.PlainText);
LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
LBL_CountOfChars_Ref.Update();
));
LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
LBL_CountOfLines_Ref.Update();
));
*/
/*reader= File.OpenText(InputTextFile);
string Line = "";
int LinesCounter = 0;
while ((Line = reader.ReadLine()) != null)
if (worker.CancellationPending)
if (reader != null)
reader.Close();
reader = null;
e.Cancel = true;
//cancel backgroundworker
return;
if(LinesCounter >0 && LinesCounter <= 1000)
RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
RichTXT_OutText_Ref.AppendText(LinesCollection);
));
LinesCollection = "";
LinesCounter = 0;
else
if (LinesCollection == "")
LinesCollection= Line;
else
LinesCollection += Environment.NewLine + Line;
LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
LBL_CountOfChars_Ref.Update();
));
LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
LBL_CountOfLines_Ref.Update();
));
//calculate progress value
ProgressPercentage = (int)Math.Round((((double)PercentageCounter) / Lines.Length) * 100);
if (ProgressPercentage < 100)
//report progress
worker.ReportProgress(ProgressPercentage);
TempCount++;
PercentageCounter++;
Tmp_Count++;
*/
if (reader != null)
reader.Close();
reader = null;
//disable Correct button because
//we are out of loop
if (BTN_Send_Ref != null)
//we call this btn Correct
//from another thread
//so we must use Invoke
BTN_Send_Ref.Invoke((MethodInvoker)(() =>
BTN_Send_Ref.Enabled = false;
));
catch (Exception ex)
if (reader != null)
reader.Close();
reader = null;
MessageBox.Show(ex.Message+"\n"+ex.StackTrace.ToString());
throw ex;
问题是代码工作正常,某些文件说小于 1000,但是当我使用大小大于 1000 的大文件时,行数少于实际文件行数
例如我附加文本文件 my text file 大小为 62077,但使用我的代码后,richtextbox 行为 62028,字符数也小于文件的实际字符数,但 Richtextbox 和文件中内容的开头和结尾是相同的,我不知道错误在哪里,我想要的只是读取文本文件,将每 1000 行合并到一个字符串中,将其附加到 Richtextbox 重复操作,直到文件内容结束。 我不想使用 Richtextbox1.LoadFile(...) 和 Richtextbox1.Text=File.ReadAllLines(file);因为他们在使用超过 39000 行的大文件时会冻结并挂起 UI, 我想知道代码有什么问题?为什么不能得到正确的结果? 我希望你能帮助我。
【问题讨论】:
这能回答你的问题吗? In C#, Loading large file into winform richtextbox How to: Load Files into the Windows Forms RichTextBox Control 你试过Richtextbox1.Lines = File.ReadAllLines(InputTextFile);
吗?
使用 Richtextbox1.Lines = File.ReadAllLines(InputTextFile) 读取 39000 行以上的大文件;即使我使用 backgroundworker 也会挂起 UI,所以我决定将每 1000 行添加到richtextbox,直到完成所有行
我不认为以 1000 为单位阅读会真正有帮助。您的用户将如何处理文本框中的 39000 行文本?
【参考方案1】:
我使用这个代码,它工作正常:
private void Worker_DoWork(object sender, DoWorkEventArgs e)
try
//
int ProgressPercentage = 0;
BTN_Send_Ref.Invoke((MethodInvoker)(() =>
BTN_Send_Ref.Enabled = true;
));
string[] Lines = File.ReadAllLines(InputTextFile);
string LinesCollection = "";
for(int x = 0; x < Lines.Length; x++)
if (worker.CancellationPending)
e.Cancel = true;
//cancel backgroundworker
return;
LinesCollection += Lines[x] + Environment.NewLine;
if (x>0 && x % 1000 == 0)
RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
RichTXT_OutText_Ref.AppendText(LinesCollection );
));
LinesCollection = "";
LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
LBL_CountOfChars_Ref.Update();
));
LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
LBL_CountOfLines_Ref.Update();
));
if(x==Lines.Length-1 && LinesCollection != "")
RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
RichTXT_OutText_Ref.AppendText(LinesCollection.TrimEnd(Environment.NewLine.ToCharArray()) );
));
LinesCollection = "";
LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
LBL_CountOfChars_Ref.Update();
));
LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
LBL_CountOfLines_Ref.Update();
));
ProgressPercentage = (int)Math.Round((((double)x) / Lines.Length) * 100);
if (ProgressPercentage < 100)
//report progress
worker.ReportProgress(ProgressPercentage);
//disable Correct button because
//we are out of loop
if (BTN_Send_Ref != null)
BTN_Send_Ref.Invoke((MethodInvoker)(() =>
BTN_Send_Ref.Enabled = false;
));
catch (Exception ex)
MessageBox.Show(ex.Message+"\n"+ex.StackTrace.ToString());
throw ex;
【讨论】:
以上是关于将大文本文件加载到richtextbox c# [重复]的主要内容,如果未能解决你的问题,请参考以下文章