2012年11月29日木曜日

Win8 ストアアプリのプログラミングでDriveInfo が使えない

Windows8 ストアーアプリで、ドライブの空き容量を一覧で表示するアプリ
(感じ的には explorer で、コンピュータ を一覧表示させた、写真のような感じ)

を作ろうと思ったのですが、なかなかうまくいきません。

Desktop アプリなら DriveInfo で、簡単に取れる、ドライブの空き容量などの情報が
Windows8 ストアーアプリでは、取得できません。
というか、 DriveInfo クラス自体がないみたいです。

ネットで調べているのですが、なかなか上手い情報が見つかりません。
ドライブの空き領域を取得する方法なんて、すぐ見つかりそうなものなのに・・・。



とりあえず、Desktop アプリ で、ドライブ情報を取得するプログラムを載せておきます。
フォーム上の、ボタンをクリックすとドライブ情報が表示されます。

Windows8 Desktop C#

Form に Button と Label を配置

Form1.cs
====================================


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Disk_Drive_Info
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            label1.Text = "";

            foreach (DriveInfo Dd in allDrives)
            {
                label1.Text += "Drive " + Dd.Name;
               
                if (Dd.IsReady == true)
                {
                    long FreeP = Dd.TotalFreeSpace * 100 / Dd.TotalSize;
                   
                    label1.Text += "  : " + FreeP.ToString() + "%  Free   ";
                    label1.Text += Dd.TotalFreeSpace / 1000000000 + "GB / ";
                    label1.Text += Dd.TotalSize / 1000000000 + "GB " + "\n\r";
                    // label1.Text += "    Volume label: " + Dd.VolumeLabel  + "\n\r";
                    // label1.Text += "    File system: " + Dd.DriveFormat + "\n\r";
                    // label1.Text += "    DriveType: " + Dd.DriveType + "\n\r";
                }
                else
                {
                    label1.Text +=  "  : " + "\n\r";
                }

            }
        }
    }
}



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



0 件のコメント:

コメントを投稿