2012年11月30日金曜日

HDDの一覧を表示するプログラム

前回に引き続き、Windows8 ストアーアプリで、ドライブの空き容量を一覧で表示するアプリ
を作ろうとしているのだけれど、問題は全然解決していません。

デバイスの一覧を表示するプログラムのサンプルが有ったので
これを基に、ハードディスクドライブ(HDD) の一覧を表示させることはできたのですが
これだと、あくまで、ハードとしての HDD の一覧なので、
必要としている、ドライブの空き容量の情報を得られませんでした。



とりあえず、今回作った、HDDの一覧を表示するプログラムを載せておきます。
ボタンをクリックすると、HDDの一覧が表示されます。

Disk drive information   Windows8 Store App C#


MainPage.xaml
====================================


<Page
    x:Class="Disk_drive_information.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Disk_drive_information"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="ContentRoot" Background="Black" Margin="100,20,100,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <StackPanel x:Name="Header" Grid.Row="0">
            <StackPanel Orientation="Horizontal"/>
        </StackPanel>

        <!-- Content -->
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="1" ZoomMode="Disabled">
            <StackPanel x:Name="ContentPanel">
                <Button x:Name="EnumerateInterfacesButton" Content="Enumerate" Margin="50,20,10,0" Click="EnumerateDeviceInterfaces" RenderTransformOrigin="0.821,0.921" HorizontalAlignment="Left" />


                <TextBlock Text="Output" Margin="0,25,0,20" />
                <StackPanel x:Name="Output"  HorizontalAlignment="Left">
                    <TextBlock x:Name="OutputText" />

                    <!-- Device Interfaces-->
                    <ListBox x:Name="DeviceInterfacesOutputList" IsEnabled="False" BorderThickness="0" />

                </StackPanel>
            </StackPanel>
        </ScrollViewer>

    </Grid>
</Page>


====================================


MainPage.xaml.cs
====================================


using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;

using Windows.Devices.Enumeration;


// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください

namespace Disk_drive_information
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// このページがフレームに表示されるときに呼び出されます。
        /// </summary>
        /// <param name="e">このページにどのように到達したかを説明するイベント データ。Parameter
        /// プロパティは、通常、ページを構成するために使用します。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        async void EnumerateDeviceInterfaces(object sender, RoutedEventArgs eventArgs)
        {
            EnumerateInterfacesButton.IsEnabled = false;
            DeviceInterfacesOutputList.Items.Clear();
            try
            {
                var selector = "System.Devices.InterfaceClassGuid:=
       {53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
                + " AND
       System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
               
                var interfaces = await DeviceInformation.FindAllAsync(selector, null);
                OutputText.Text = interfaces.Count + " device interface(s) found\n\n";
                foreach (DeviceInformation deviceInterface in interfaces)
                {
                    DisplayDeviceInterface(deviceInterface);
                }
            }
            catch (ArgumentException)
            {
                OutputText.Text = "Not";
            }
            EnumerateInterfacesButton.IsEnabled = true;
        }

        async void DisplayDeviceInterface(DeviceInformation deviceInterface)
        {
            var id = "Id: " + deviceInterface.Id;
            var name = "name: "+ deviceInterface.Name;
            var isEnabled = "IsEnabled: " + deviceInterface.IsEnabled;
           
            var item = id + "\n" + name + "\n" + isEnabled + "\n" ;
            DeviceInterfacesOutputList.Items.Add(item);
        }

    }

}


====================================

0 件のコメント:

コメントを投稿