转:
您可以通过查询WMI对象来获取驱动器类型。下面是一段例子代码: SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query); foreach(ManagementBaseObject disk in searcher.Get())
{
this.textBox1.Text+="\r\n"+disk["Name"]+" "+disk["DriveType"];
}这段代码在我的计算机上的执行结果如下:C: 3
D: 5
E: 2
Q: 4
R: 4
S: 4
T: 4
U: 4
V: 4
W: 4
X: 4
Y: 4其中各个数字代表的意义如下:1 No type
2 Floppy disk
3 Hard disk
4 Removable drive or network drive
5 CD-ROM
6 RAM disk 这样就可以得知各个驱动器的类型,例如C:是本地硬盘,D:是CD-ROM(实际上是一个DVD/CD-R Combo),而从Q:往后都是网络硬盘。
您可以通过查询WMI对象来获取驱动器类型。下面是一段例子代码: SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query); foreach(ManagementBaseObject disk in searcher.Get())
{
this.textBox1.Text+="\r\n"+disk["Name"]+" "+disk["DriveType"];
}这段代码在我的计算机上的执行结果如下:C: 3
D: 5
E: 2
Q: 4
R: 4
S: 4
T: 4
U: 4
V: 4
W: 4
X: 4
Y: 4其中各个数字代表的意义如下:1 No type
2 Floppy disk
3 Hard disk
4 Removable drive or network drive
5 CD-ROM
6 RAM disk 这样就可以得知各个驱动器的类型,例如C:是本地硬盘,D:是CD-ROM(实际上是一个DVD/CD-R Combo),而从Q:往后都是网络硬盘。
比如
我的电脑-
+- C: +- D:就像我get C:\下的子目录是一样的效果
<%@Import Namespace="System"%>
<%@Import Namespace="System.IO"%>
<%@Import Namespace="System.Data"%>
<link href="fyz.css" rel="stylesheet" type="text/css">
<script runat=server>
string[] LocalDrivers;
void Page_Load(object sender,EventArgs e){
LocalDrivers=Directory.GetLogicalDrives();
Response.Write("<b>There are "+ LocalDrivers.Length +" drivers.</b><br>");
DataSet ds=new DataSet("Dirvers");
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("Drivers",typeof(string)));
DataRow dr;
foreach(string driver in LocalDrivers){
dr=dt.NewRow();
dr[0]=driver;
dt.Rows.Add(dr);
}
//DataSet.Tables.Add(dt);
Drivers.DataSource=new DataView(dt);
Drivers.DataBind();
}
</script>
<form runat="server">
<asp:DataList id=Drivers runat="server" Class="txt" BorderColor="#668899"
GridLines="None" Width="100"
HorizontalAlign="Center">
<ItemTemplate >
<asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "Drivers")%>'
NavigateUrl='<%#string.Format("Files.aspx?path={0}",Server.UrlEncode((string)DataBinder.Eval(Container.DataItem, "Drivers")))%>'
Target="file" runat="server" />
</ItemTemplate>
</asp:DataList>
</form>
谢谢了
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Drivers
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.ListBox listBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
string CurrentDirectory=null;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.listBox3 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(96, 158);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// listBox2
//
this.listBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(120, 8);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(208, 158);
this.listBox2.TabIndex = 2;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// listBox3
//
this.listBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.listBox3.ItemHeight = 12;
this.listBox3.Location = new System.Drawing.Point(336, 8);
this.listBox3.Name = "listBox3";
this.listBox3.Size = new System.Drawing.Size(208, 266);
this.listBox3.TabIndex = 3;
this.listBox3.SelectedIndexChanged += new System.EventHandler(this.listBox3_SelectedIndexChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 176);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 4;
this.label1.Text = "文件路径";
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(16, 200);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(304, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(248, 264);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 6;
this.button1.Text = "退出";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 232);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 7;
this.label2.Text = "当前路径:";
//
// textBox2
//
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox2.Location = new System.Drawing.Point(16, 256);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(216, 21);
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(552, 293);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox3);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
DirectoryInfo dinfo=new DirectoryInfo("D:\\");
//dinfo.GetType().FullName
string[] LocalDrivers=Directory.GetLogicalDrives();
for(int i=0;i<LocalDrivers.Length;i++)
listBox1.Items .Add ((object)(LocalDrivers[i]));
} private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{ CurrentDirectory=(string)listBox1.SelectedItem;
textBox2.Text =CurrentDirectory;
try
{
string[] paths=Directory.GetDirectories( CurrentDirectory);
string[] files=Directory.GetFiles(CurrentDirectory);
listBox2.Items.Clear();
listBox3.Items.Clear();
for(int i=0;i<paths.Length;i++)
listBox2.Items.Add((object) GetPathName(paths[i]));
for(int i=0;i<files.Length;i++)
listBox3.Items.Add((object)Path.GetFileName(files[i]));
}
catch(Exception ee)
{
listBox2.Items.Clear();
listBox2.Items.Add ((object)ee.Message);
} } private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{ CurrentDirectory=Path.Combine( CurrentDirectory,(string)listBox2.SelectedItem);
textBox2.Text =CurrentDirectory;
try
{
string[] paths=Directory.GetDirectories(CurrentDirectory);
string[] files=Directory.GetFiles(CurrentDirectory);
listBox2.Items.Clear();
listBox3.Items.Clear();
for(int i=0;i<paths.Length;i++)
listBox2.Items.Add((object)GetPathName(paths[i]));
for(int i=0;i<files.Length;i++)
listBox3.Items.Add((object)Path.GetFileName(files[i]));
}
catch(Exception ee)
{
listBox2.Items.Clear();
listBox2.Items.Add ((object)ee.Message);
} } private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
} private void listBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
textBox1.Text =Path.Combine(CurrentDirectory,(string)listBox3.SelectedItem);
}
private string GetPathName(string path){
string[] pa=path.Split('\\');
return (pa.Length==0?path:pa[pa.Length -1]);
}
} }