1

我在我的代码中使用 DriveInfo.GetDrives() 方法来填充一个组合框,其中包含我指定计算机上所有可用和准备好的可移动驱动器。它在三台测试计算机上运行良好,但在单台计算机上,当用户单击打开带有组合框的窗口的按钮(以及构造函数中的 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

1 回答 1

2

问题机器中的一个驱动器很可能处于非活动模式,需要几秒钟才能启动。(我的家用机器上也有同样的问题)

于 2013-07-22T22:32:26.563 回答