我想要做的是每次讀取文件時更新UI上的文件數。同樣,它甚至沒有顯示用戶界面上的文件總數,當用戶點擊browseFolder事件的確定按鈕。任何幫助,將不勝感激。下面是我的代碼:如何在Windows應用程序的用戶界面上顯示filecount
private void browse_Click(object sender, EventArgs e)
{
try
{
int fileCount = 0;
string newFileName1 = "";
string newFileName2 = "";
week = textBox2.Text;
if (week == null || week == "")
{
MessageBox.Show("Week cannot be null.");
return;
}
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
int j = 0;
int totalNoOfFiles = 0;
DateTime starttime = DateTime.Now;
string folderPath = folderBrowserDialog1.SelectedPath;
string folderName = Path.GetFileName(folderBrowserDialog1.SelectedPath);
totalNoOfFiles = Directory.GetFiles(folderPath, "*.txt", SearchOption.AllDirectories).Length;
lbltotalfiles.Text = Convert.ToString(totalNoOfFiles);
progressBar1.Minimum = 0;
progressBar1.Maximum = totalNoOfFiles;
DirectoryInfo dInfo = new DirectoryInfo(folderPath);
foreach (DirectoryInfo folder in dInfo.GetDirectories())
{
newFileName1 = "Files_with_dates_mismatching_the_respective_week_" + folder.Name + ".txt";
newFileName2 = "Files_with_wrong_date_format_" + folder.Name + ".txt";
if (File.Exists(folderPath + "/" + newFileName1))
{
File.Delete(folderPath + "/" + newFileName1);
}
if (File.Exists(folderPath + "/" + newFileName2))
{
File.Delete(folderPath + "/" + newFileName2);
}
FileInfo[] folderFiles = folder.GetFiles();
if (folderFiles.Length != 0)
{
List<Task> tasks = new List<Task>();
foreach (var file in folderFiles)
{
fileCount = ++fileCount;
lblFilesRead.Text = Convert.ToString(fileCount);
progressBar1.Value = ++j;
var task = ReadFile(file.FullName, folderPath, folder.Name, week);
tasks.Add(task);
}
Task.WhenAll(tasks.ToArray());
DateTime stoptime = DateTime.Now;
TimeSpan totaltime = stoptime.Subtract(starttime);
label6.Text = Convert.ToString(totaltime);
textBox1.Text = folderPath;
}
}
DialogResult result2 = MessageBox.Show("Read the files successfully.", "Important message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception)
{
throw;
}
}