WPF DataGrid Get Drive Information


This entry is part 6 of 6 in the series WPF DataGrid Control

The DataGrid is a good candidate for displaying basic hard drive information because the rows are created automatically. When you enumerate the drives, you cannot know for certain how many drives you have on the system. Users may have hard drives and USB external drives or other devices such as smartphones or tablets that show up as drives.

Here is a simple example program. The DriveInfo.GetDrives Method retrieves the drive names of all logical drives on a computer. This method retrieves all logical drive names on a computer. You can use this information to iterate through the array and obtain information on the drives using other DriveInfo methods and properties. Use the IsReady property to test whether a drive is ready because using this method on a drive that is not ready will throw a IOException.

The XAML code is very simple.

<Window x:Class="DriveInformation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DriveInformation"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="DriveInformation" Height="450" Width="800">
    <StackPanel>
        <Button Click="Button_Click">Get Drive Info</Button>
        <DataGrid Name="dtagrid"/>
    </StackPanel>
</Window>

The C# code behind.

using System.Collections.Generic;
using System.Windows;
using System.IO;
namespace DriveInformation
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            List<DrvInfo> dlist = new List<DrvInfo>();
            foreach (DriveInfo d in allDrives)
            {
                if (d.IsReady == true)
                {
                    dlist.Add(new DrvInfo()
                    {
                        Name = d.Name,
                        VolumeLabel = d.VolumeLabel,
                        DriveFormat = d.DriveFormat,
                        AvailableFreeSpace = d.AvailableFreeSpace,
                        TotalFreeSpace = d.TotalFreeSpace,
                        TotalSize = d.TotalSize
                    });
                }
            }
            dtagrid.ItemsSource = dlist;
        }
    }
    public class DrvInfo
    {
        public string Name { get; set; }
        public string VolumeLabel { get; set; }
        public string DriveFormat { get; set; }
        public long AvailableFreeSpace { get; set; }
        public long TotalFreeSpace { get; set; }
        public long TotalSize { get; set; }
    }
}
Series Navigation<< WPF Weather API with DataGrid