计算每个文件大小的进度条的最大值

时间:2016-01-26 08:53:31

标签: c# file progress-bar

我正在逐行阅读文件。

在我的GUI中,我有一个ProgressBar。但是我想计算ProgressBar的最大值作为文件的大小,而不是按照我正在阅读的行计算。

如何计算ProgressBar的值?根据我正在阅读的每个数据后的大小?

感谢。

1 个答案:

答案 0 :(得分:3)

您可以通过获取FileInfo.Length

来获取文件大小
FileInfo fi = FileSystem.GetFileInfo(filepath);

然后基于此,你可以通过比较你读过的字符数来将其翻译成ProgressBar.Value

int charRead = 0;
foreach(string line in lines){ //get lines whichever way you want
    charRead += line.Length + 1; //+1 is for the \n character you throw
    double progress = (double)charRead / fi.Length;
    //compare charRead with fi.Length
}