progressBar when reading the txt document (file)

你。 提交于 2020-07-10 10:25:25

问题


Hi I've made a MD5 AntiVirus Checker. There is one thing i need help with and thats the progressBar. I want the progressBar to progress along when Visual Reads the txt file.

Edit: Done this, it has a total of 34801907 lines. Could you possible complete the code cause I've got errors like

(Severity Code Description Project File Line Suppression State Error CS0019 Operator '!=' cannot be applied to operands of type 'method group' and 'string' ANTI-VIRUS F:\Program Files\ms projects\ ANTI-VIRUS\Form3.cs 176 Active)

Code:

    private void progressBar1_Click(object sender, EventArgs e)
    {
        int lineCount = File.ReadAllLines(@".\AntiChecker.txt").Length;
        progressBar1.Maximum = lineCount ; //set maximum value to lines 
        int readLines = 34801907;
        using (StreamReader sr = new StreamReader(@".\AntiChecker.txt"))
        {
            while (sr.ReadLine != String.Empty)
            {
                readLines++;
                progressBar1.Value = readLines;
            }
        }
    }
}

}


回答1:


To be able to correctly update an ProgressBar (i.e. to evenly update it every times it is updated) we need to get the number of lines of the text file. If this is not a fixed value and is different in every case we encounter some problems.

However since your file always contains 34801907 lines you may simply put

int lineCount = 34801907;

To then read the file and update the progressbar use a StreamReader

I am assuming your ProgressBar is named progressBar1.

First set the max value of your ProgressBar to the number of lines in the file. Then loop through every line of the file, read it and then increment the ProgressBar that one more line has been read.

private void button2_Click(object sender, EventArgs e)
    {
        progressBar1.Value = 0; //if you want to make sure that the progressBar starts at 0        
        progressBar1.Maximum = lineCount; //set maximum value to lines in file
        int readLines = 0; //Counter that gives the total of lines that has been read        
        List<string> md5signatures = new List<string>(); //list that stores all the read lines
       
        string line = ""; //stores the current line

        using(StreamReader sr = new StreamReader(@".\AntiChecker.txt")) {
           while(line = sr.ReadLine() != String.Empty) {
              readLines++;
              progressBar1.Value = readLines;
                                            //Add progressBar1.Value = readLines -1; here if progressbar do not increment completely
              md5signatures.Add(line);
           }
        }

        if (md5signatures.Contains(md5box.Text))
        {
            Status.Text = "Infected!";
            Status.ForeColor = Color.Red;
            listBox1.Items.Add(selectedFileName)            
        }

        else
        {
            Status.Text = "Clean!";
            Status.ForeColor = Color.Green;
        }           
    }

NOTE

There exists a bug in WinForms where the ProgessBar appears to be slightly lagging behind, which means that it may not increment all the way before the process is complete. To fix this add

progressBar1.Value = readLines-1;

after you are setting the value of the ProgressBar to readLines in the while loop above.

AND

If your ProgressBar is not refreshing correctly you may write

progressBar1.refresh();

in the while loop.

Bonus

If you want a Label to display the progress (in percentage) next to the ProgressBar you may calculate the current progress at every step in the while loop by

int percentage = (int)( ( readLines / lineCount ) * 100.0 );

and then update the text of the Label at every step in the loop by

label1.text = percentage + "%";

assuming your Label is named label1. However to correctly update the Label you need to refresh it at every iteration in the while loop. This can be done by the line

this.Refresh();


来源:https://stackoverflow.com/questions/62813908/progressbar-when-reading-the-txt-document-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!