您的位置:首页 > 移动开发 > Objective-C

读取驱动器信息

2009-06-15 17:35 204 查看
除了处理文件和目录之外,net framework 2.0 还可以从指定的驱动器中读取信息。这是使用DriveInfo类实现的。

DriveInfo类可以扫描系统,提供可用驱动器的列表,还可以进一步提供驱动器的大量细节。

 

为了演示DriverInfo类的用法,创建一个简单的widows窗体,列出计算机上的所有驱动器,再提供用户选择的驱动器的信息。

windows窗体包含一个简单的ListBox,如图一所示

 

建立好窗体后其代码包含两个事件,一个在窗体加载时引发,另一个在最终用户从列表中选择驱动器时引发。代码如下:

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

         

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            DriveInfo[] di = DriveInfo.GetDrives();

            foreach (DriveInfo itemDrive in di)

            {

                listBox1.Items.Add(itemDrive.Name);

            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            DriveInfo di = new DriveInfo(listBox1.SelectedIndex.ToString());

            MessageBox.Show("Available Free Space:"+di.AvailableFreeSpace+"/n"+

                "Driver Format:"+di.DriveFormat+"/n"+

                "Driver Type:"+di.DriveType+"/n"+

                "Is Ready:"+di.IsReady.ToString()+"/n"+

                "Name:"+di.Name+"/n"+

                "Root Directory:"+di.RootDirectory+"/n"+

                "ToString() Value:"+di.ToString()+"/n"+

                "Total Free Space:"+di.TotalFreeSpace+"/n"+

                "Volume Lable:"+di.VolumeLabel.ToString(),di.Name+"DRIVER INFO" );

        }

    }

}

 

 

 

 

可是我在运行中发生这个错误

 

应用程序中发生了无法处理的异常。.....应用程序将立即关闭。

 

对象必须是根目录("c:/")或驱动器号("c")..

 

问题没有得到解决 晚上再说吧。

 

问题解决了 是取值出了问题

 

出错的代码在

 

DriveInfo di = new DriveInfo(listBox1.SelectedIndex.ToString());

 

我去取得是数组的Index 应该是里面的值才对 加上一句 再修改一下

 

修正源码为:

 

            string strTmp = listBox1.SelectedItem.ToString();

            DriveInfo di = new DriveInfo(strTmp.Substring(0, strTmp.Length - 2));

 

OK! 可以取得磁盘信息了,没有再出错。接着要做的是

 

如何读取U盘的文件信息了!继续前进!

 

 
和 "读取驱动器信息" 有关的编程小帖士:
strong>String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); 

取远程用户IP地址

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息