2016-02-22 42 views
0

我想要做的是每次讀取文件時更新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; 
     } 
    } 

回答

0

當你正在閱讀的你回採Windows事件隊列,負責與用戶/重繪組件的交互/處理文件。您應該不時撥打電話:Application.DoEvents()方法。它將允許應用程序處理消息,如更新標籤文本。

如果您想要一個非常好的用戶體驗,您應該使用另一個線程來處理文件,並從該線程使用label.BeginInvoke方法來更新表單上的標籤。這裏是很好的教程:http://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke

0

你也可以看看FileSystemWatcher類。從長遠來看,這可能容易維護...

class Program 
{ 
    static void Main(string[] args) 
    { 
     var watcher = new System.IO.FileSystemWatcher() 
     { 
      Path = @"G:\cc\", 
     }; 

     watcher.Created += Watcher_Created; 
     watcher.EnableRaisingEvents = true; 

     Console.ReadKey(); 
    } 

    private static void Watcher_Created(
     object sender, 
     FileSystemEventArgs e 
     ) 
    { 
     var folder = new DirectoryInfo(e.FullPath) 
      .Parent; 

     Console.WriteLine(
      folder? 
       .GetFiles() 
       .Length 
      ); 
    } 
} 
相關問題