gpt4 book ai didi

c# - 为 StreamReader 添加进度条监控?

转载 作者:行者123 更新时间:2023-11-30 20:20:11 25 4
gpt4 key购买 nike

我正在 Visual Studio 2015 中编写一个相当简单的 Windows 窗体 C# 应用程序。这是我的一个处理程序:

private void button2_Click(object sender, EventArgs e)
{
listBoxCodes.Items.Clear();
listBoxDuplicates.Items.Clear();

Cursor.Current = Cursors.WaitCursor;
Application.DoEvents();

progressBar.Value = 0;
using (var reader = new StreamReader(textBoxGENIO.Text))
{
// progressBar is set for 5 unit intervals (from 0 to 100)

// How can I show % complete reading the file?

string line;
while ((line = reader.ReadLine()) != null)
{
if(line.Length > 8 && line.Substring(0, 4) == "080,")
{
string strCode = line.Substring(4, 4);

if (listBoxCodes.FindStringExact(strCode) == -1)
listBoxCodes.Items.Add(strCode);
else
listBoxDuplicates.Items.Add(strCode);
}
}
}

Cursor.Current = Cursors.Default;
}

现在,我明白了,我可以读一遍文件,得到总行数,然后再读一遍文件,根据行数百分比做进度监控。

但我想避免读取文件两次,因为它们会变得非常大。

有什么方法可以使用文件位置/文件大小来确定完成百分比?

我在 StreamReader 对象中看不到任何 Position 方法。

谢谢。

更新:目前正在查看这个可能是答案的问题:

Tracking the position of the line of a streamreader

更新:

我的计算好像有误:

private void button2_Click(object sender, EventArgs e)
{
listBoxCodes.Items.Clear();
listBoxDuplicates.Items.Clear();

Cursor.Current = Cursors.WaitCursor;
Application.DoEvents();

int iLastPercentage = -1;

progressBar.Value = 0;
using (var reader = new StreamReader(textBoxGENIO.Text))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// progressBar is set for 5 unit intervals (from 0 to 100)

// How can I show % complete reading the file?
int iPercentage = Convert.ToInt32(((double)reader.BaseStream.Length / 100.0) * (double)reader.BaseStream.Position);
if (iLastPercentage == -1 || (iPercentage - iLastPercentage >= 5))
{
progressBar.PerformStep();
iLastPercentage = iPercentage;
Application.DoEvents();
}

if (line.Length > 8 && line.Substring(0, 4) == "080,")
{
string strCode = line.Substring(4, 4);

if (listBoxCodes.FindStringExact(strCode) == -1)
listBoxCodes.Items.Add(strCode);
else
listBoxDuplicates.Items.Add(strCode);
}
}
}

Cursor.Current = Cursors.Default;
}

最佳答案

看看下面是否适合你:

private void button2_Click(object sender, EventArgs e)
{
listBoxCodes.Items.Clear();
listBoxDuplicates.Items.Clear();

Cursor.Current = Cursors.WaitCursor;
Application.DoEvents();

progressBar.Value = 0;
using (var reader = new StreamReader(textBoxGENIO.Text))
{
// progressBar is set for 5 unit intervals (from 0 to 100)

// How can I show % complete reading the file?
Stream baseStream = reader.BaseStream;
long length = baseStream.Length;

string line;
while ((line = reader.ReadLine()) != null)
{
if(line.Length > 8 && line.Substring(0, 4) == "080,")
{
string strCode = line.Substring(4, 4);

if (listBoxCodes.FindStringExact(strCode) == -1)
listBoxCodes.Items.Add(strCode);
else
listBoxDuplicates.Items.Add(strCode);
}

progressBar.Value = baseStream.Position / length * 100;
// Add code to update your progress bar UI
}
}

Cursor.Current = Cursors.Default;
}

关于c# - 为 StreamReader 添加进度条监控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com