读取驱动器信息
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地址
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地址
相关文章推荐
- javascript读取本地驱动器信息
- C#读取驱动器信息
- Wmi的Win32_Volume 磁盘卷列表 Win32_DiskDrive 物理磁盘驱动器 逻辑分区的信息读取(Win32_LogicalDisk class)
- java读取图片的EXIF信息
- delphi 读取编译的version信息
- ABAP-读取应用服务器目录信息的函数代码摘抄
- poi读取损坏excel的异常信息
- [置顶] I/O流读取文本中学生成绩并统计符合条件的学生信息
- Java读取相片信息
- dedecms读取多个类别信息
- Java Properties 类读取配置文件信息
- 第四十三篇:Imaging (previously called Sanselan)读取图像信息
- socket监听端口读取端口信息
- 静态页面利用JS读取cookies记住用户信息
- 用C#读取注册表信息
- Android读取手机联系人信息
- 【转载】使用Apache Commons Configuration读取配置信息
- ContentProvider基础(二)读取联系人信息
- VC实现访问网站,读取信息
- Android工程模式下读取硬件信息