如何在文件修改程序中使用进度条,每次处理文件时进度条都会增加,当处理完所有文件时它会达到全部值,即100我尝试了类似的东西
string files=Directory.GetFiles(path,"*.txt",SearchOption.AllDirectories);
foreach (var file in files) {
progressBar1.Value=progressBar1.Value+100/(files.Length);
//Do some modification
}
MessageBox.Show("Done");
Run Code Online (Sandbox Code Playgroud)
但即使在处理完所有文件后,进度条的值仍然不满,我做错了什么?
您需要设置ProgressBar.Maximum属性以匹配文件数,然后为循环的每次迭代将值递增1:
var files = Directory.GetFiles(path,"*.txt",SearchOption.AllDirectories);
progressBar1.Maximum = files.Length;
foreach (var file in files) {
progressBar1.Value = progressBar1.Value+1;
//Do some modification
}
MessageBox.Show("Done");
Run Code Online (Sandbox Code Playgroud)