为 StreamReader 添加进度条监控?

Adding progress bar monitoring for StreamReader?

我正在 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;
}

这是正确的方法,您必须将其类型转换为 double

private void button_Load_Int_Click(object sender, EventArgs e)
    {
        Application.DoEvents();
        progressBar.Value = 0;
        using (StreamReader sr = new StreamReader(pathInt))
        {
            Stream baseStream = sr.BaseStream;
            long length = baseStream.Length;
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                string strCode = line.Substring(4, 4);

                if (listBoxCodes.FindStringExact(strCode) == -1)
                    listBoxCodes.Items.Add(strCode);
                else
                    listBoxDuplicates.Items.Add(strCode)
                progressBar.Value = Convert.ToInt32((double)baseStream.Position / length * 100);
                Application.DoEvents();
            }
        }
    }