如题.
我导入一个文本文件,速度很慢,象死机一样的,不知道为什么?
这段代码不合适吗?能优化一下吗?
string path = "list.txt";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader m_sr = new StreamReader(fs);
m_sr.BaseStream.Seek(0, SeekOrigin.Begin);
this.textBox1.Text = "";
string strLine = m_sr.ReadLine();
while (strLine != null)
{
this.textBox1.Text += strLine + "\n";
strLine = m_sr.ReadLine();
}
m_sr.Close();
Vince Yuan
01-26-2007, 02:42 AM
ȣֳÿؼԲܳ˵Чʼ͵----ˡ
ȻӦÿĴʲôطʱ
Ľ룺
һд
this.textBox1.Text = m_sr.ReadToEnd();
滻д룬ѭ
this.textBox1.Text = "";
string strLine = m_sr.ReadLine();
while (strLine != null)
{
this.textBox1.Text += strLine + "\n";
strLine = m_sr.ReadLine();
}
Vince Yuan
"" <@discussions.microsoft.com> wrote in message
news:642D453C-5D9A-414E-AD98-F1D8DC244BAB@microsoft.com...
> .
>
> ҵһıļ,ٶȺ,һ,֪Ϊʲô?
>
> δ벻?Żһ?
>
> string path = "list.txt";
> FileStream fs = new FileStream(path, FileMode.OpenOrCreate,
> FileAccess.Read);
>
> StreamReader m_sr = new StreamReader(fs);
> m_sr.BaseStream.Seek(0, SeekOrigin.Begin);
> this.textBox1.Text = "";
> string strLine = m_sr.ReadLine();
> while (strLine != null)
> {
> this.textBox1.Text += strLine + "\n";
> strLine = m_sr.ReadLine();
> }
> m_sr.Close();
>
谢谢!
我找找原因.
“Vince Yuan”编写:
> 首先,这种常用控件绝对不可能出现你说的这种效率极低的情况----如果真这样,微软早完了。
> 然后,你应该看看你的代码在什么地方花的时间最长。
> 最后,改进代码:
> 用一行代码
> this.textBox1.Text = m_sr.ReadToEnd();
> 替换六行代码,抛弃循环
> this.textBox1.Text = "";
> string strLine = m_sr.ReadLine();
> while (strLine != null)
> {
> this.textBox1.Text += strLine + "\n";
> strLine = m_sr.ReadLine();
> }
>
> Vince Yuan
>
> "虎劲风" <@discussions.microsoft.com> wrote in message
> news:642D453C-5D9A-414E-AD98-F1D8DC244BAB@microsoft.com...
> > 如题.
> >
> > 我导入一个文本文件,速度很慢,象死机一样的,不知道为什么?
> >
> > 这段代码不合适吗?能优化一下吗?
> >
> > string path = "list.txt";
> > FileStream fs = new FileStream(path, FileMode.OpenOrCreate,
> > FileAccess.Read);
> >
> > StreamReader m_sr = new StreamReader(fs);
> > m_sr.BaseStream.Seek(0, SeekOrigin.Begin);
> > this.textBox1.Text = "";
> > string strLine = m_sr.ReadLine();
> > while (strLine != null)
> > {
> > this.textBox1.Text += strLine + "\n";
> > strLine = m_sr.ReadLine();
> > }
> > m_sr.Close();
> >
>
>
>
lander
02-06-2007, 04:01 AM
һζĻ,"/n"ıô,¥˼Ҫس ,ȫĻ,سǿܻʧȥ.
--
-----------------------------
Best Regards!
Lander
˾
Tianjin Microsoft Technology Center
йлԷҵ̩ɫҵG-10
Zip:300384
Tel:022-85689178/85689179-8029
Fax:85689177
----------------------------
"Vince Yuan" <shinebean@citiz.net> дϢ:u5ZoUCSQHHA.1908@TK2MSFTNGP05.phx.gbl...
> ҲǸ
> һȫı
>