在 C# 中读/写文本文件进度条 [英] Read/Write text file progressbar in C#

查看:117
本文介绍了在 C# 中读/写文本文件进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以读取一个大文本文件,分割一部分(从给定的开始和结束),并将分割的数据保存到另一个文本文件中.由于文件太大,我需要添加一个进度条在将拆分的文本写入另一个文件时读取流和另一个流.Ps.start 和 end 被赋予日期时间!!

I have a function which reads a big text file,splits a part(from a given start and end),and save the splitted data into another text file.since the file is too big,i need to add a progressbar when reading the stream and another one when writing the splitted text into the other file.Ps.start and end are given datetime!!

using (StreamReader sr = new StreamReader(file,System.Text.Encoding.ASCII))
{
    while (sr.EndOfStream == false)
    {
        line = sr.ReadLine();

        if (line.IndexOf(start) != -1)
        {
            using (StreamWriter sw = new StreamWriter(DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + "cut"))
            {
                sw.WriteLine(line);
                while (sr.EndOfStream == false && line.IndexOf(end) == -1)
                {
                    line = sr.ReadLine();
                    sw.WriteLine(line);
                }
            }

            richTextBox1.Text += "done ..." + "\n";
            break;
        }
    }
}

推荐答案

首先要做的是计算出文件使用 FileInfo 的时间,

The first thing to do would be to work out how long the file is using FileInfo,

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

FileInfo fileInfo = new FileInfo(file);
long length = fileInfo.Length;

我建议你这样做,

private long currentPosition = 0;

private void UpdateProgressBar(int lineLength)
{
    currentPosition += line.Count; // or plus 2 if you need to take into account carriage return
    progressBar.Value = (int)(((decimal)currentPosition / (decimal)length) * (decimal)100);
}

private void CopyFile()
{
    progressBar.Minimum = 0;
    progressBar.Maximum = 100;

    currentPosition = 0;

    using (StreamReader sr = new StreamReader(file,System.Text.Encoding.ASCII))
    {
        while (sr.EndOfStream == false)
        {
            line = sr.ReadLine();
            UpdateProgressBar(line.Length);

            if (line.IndexOf(start) != -1)
            {
                using (StreamWriter sw = new StreamWriter(DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + "cut"))
                {
                    sw.WriteLine(line);
                    while (sr.EndOfStream == false && line.IndexOf(end) == -1)
                    {
                        line = sr.ReadLine();
                        UpdateProgressBar(line.Length);
                        sw.WriteLine(line);
                    }
                }

                richTextBox1.Text += "done ..." + "\n";
                break;
            }
        }
    }
}

这是计算已读取文件的百分比并将进度条设置为该值.那就不用担心长度是不是长了,进度条用的是int.

Which is calculating the percentage of the file that has been read and setting the progress bar to that value. Then you don't have to worry about whether the length is a long, and the progress bar uses int.

如果您不想截断值,请执行此操作(转换为上面的 int 将始终截断小数,从而向下舍入),

If you don't want to truncate the value then do this (casting to an int above will always truncate the decimals, and thus round down),

progressBar.Value = (int)Math.Round(((decimal)currentPosition / (decimal)length) * (decimal)100), 0);

这是在后台线程上吗?不要忘记你必须调用 this.Invoke 来更新进度条,否则你会得到一个跨线程异常.

Is this on a background thread? Don't forget that you will have to call this.Invoke to update the progress bar or else you will get a cross thread exception.

这篇关于在 C# 中读/写文本文件进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆