C#DriveInfo.GetDrives()在选定的计算机上运行缓慢

时间:2013-07-22 22:19:08

标签: c# wpf xaml driveinfo getdrivetype

我在代码中使用DriveInfo.GetDrives()方法在我指定的计算机上填充所有可用且可用的可移动驱动器的组合框。它在三台测试计算机上运行良好,但在一台计算机上,当用户单击打开窗口的按钮时,其中包含组合框(以及构造函数中的GetDrives),在窗口打开之前需要几秒钟。 / p>

计算机正在运行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();
    }
}

1 个答案:

答案 0 :(得分:2)

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