2013-07-22 41 views
1

我在我的代碼中使用DriveInfo.GetDrives()方法來填充指定計算機上的所有可用和可移動驅動器的組合框。它在三臺測試計算機上運行良好,但在單臺計算機上,當用戶單擊打開包含組合框的窗口的按鈕(以及構造函數中的GetDrives)時,窗口打開前需要幾秒鐘。C#DriveInfo.GetDrives()在選定的計算機上慢

電腦運行的是Windows 7,唯一需要注意的是它有一個RAID設置。

一旦打開它響應它只是因爲某些原因打開時掛起。我在MSDN文檔中找不到任何幫助,並且在網上找不到類似的案例。如果您有任何類似問題或任何建議的經驗,請讓我知道。

我提取了使用我的項目中的DriveInfo並構建了一個測試應用程序的窗口。後面的代碼低於:

public partial class MainWindow : Window 
{ 
    //Instance variables used in class and refrenced in 'get' methods 
    int count; 
    string[] driveNames; 

    public MainWindow() //Constructor 
    { 
     InitializeComponent(); 
     getInfo(); //Populate instance vars 
    } 

    public string[] getRemovableDrives() //Returns array of drive letters for removable drives in computer 
    { 
     return driveNames; 
    } 

    public int getRemovableDrivesCount() //Returns number of removable drives in computer 
    { 
     return count; 
    } 

    private void getInfo() //Run once to get information about removable drives on computer and store into instance vars 
    { 
     count = 0; 
     List<string> drivesTemp = new List<string>(); 

     foreach (DriveInfo d in DriveInfo.GetDrives()) 
     { 
      if (d.IsReady == true && d.DriveType == DriveType.Removable && d.DriveFormat == "FAT32") 
      { 
       drives.Items.Add(d.VolumeLabel + " (" + d.Name + ")"); 
       drivesTemp.Add(d.Name); 
       count++; 
      } 
     } 

     driveNames = new string[count]; 
     for (int i = 0; i < count; i++) 
     { 
      driveNames[i] = drivesTemp[i]; 
     } 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) //Selects first available drive in drop down box 
    { 
     drives.SelectedIndex = drives.Items.Count - 1; 
    } 

    private void format_Click(object sender, RoutedEventArgs e) //Attempts to format drive 
    { 
     string drive = driveNames[drives.SelectedIndex]; 

     try 
     { 
      Directory.CreateDirectory(drive + "LOOKOUT.SD"); 
      Directory.CreateDirectory(drive + "LOOKOUT.SD\\CONFIG"); 
      Directory.CreateDirectory(drive + "LOOKOUT.SD\\HISTORY"); 
      Directory.CreateDirectory(drive + "LOOKOUT.SD\\TEST"); 
      Directory.CreateDirectory(drive + "LOOKOUT.SD\\UPDATES"); 
      Directory.CreateDirectory(drive + "LOOKOUT.SD\\VPROMPTS"); 

      MessageBox.Show("Format complete, your removable device is now ready to use.", "Format Successful", MessageBoxButton.OK, MessageBoxImage.Information); 
     } 
     catch 
     { 
      MessageBox.Show("Your removable device has failed to format correctly.", "Format Failure", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
     } 

     Close(); 
    } 

    private void cancel_Click(object sender, RoutedEventArgs e) //Closes window without formatting 
    { 
     Close(); 
    } 
} 
+4

它是反覆緩慢,還是隻有第一次?我想象也許這些驅動器是不活動的,並且在延遲期間正在旋轉。 –

+0

反覆慢,我會發布我的代碼 –

回答

2

這是極有可能的是,在出現問題的機器一個驅動器處於非活動模式,並需要幾秒鐘旋轉起來。 (我在我的家用機器上也有這個問題)

+0

其實Jonathon Reinhart在評論中說同樣的事情...我會讓這個維基答案。 –

+0

它重複發生,是否有可能驅動器繼續回到非活動狀態。我將在上面發佈我的代碼。 –

相關問題