我在代码中使用DriveInfo.GetDrives()方法,用指定计算机上所有可用且现成的可移动驱动器填充组合框。它在三台测试计算机上运行良好,但在一台计算机上,当用户单击打开带有组合框的窗口的按钮(以及构造函数中的GetDrives)时,需要几秒钟的时间才能打开窗口。
这台电脑运行的是Windows7,唯一需要注意的是它有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();
}
}
问题机器中的一个驱动器很可能处于非活动模式,需要几秒钟才能启动。(我在我的家用机器上也有同样的问题)