用C#取电脑所有的磁盘和每个磁盘下的所有目录 我要的效果是程序取电脑所有的磁盘和每个磁盘下的所有目录一加载,而不是点击某个节点再添加该节点下的目录.希望各位大哥大姐多多帮忙,小弟在这里万分感谢. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.IO;class Test{ public static void Main() { DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { Console.WriteLine("Drive {0}", d.Name); Console.WriteLine(" File type: {0}", d.DriveType); if (d.IsReady == true) { Console.WriteLine(" Volume label: {0}", d.VolumeLabel); Console.WriteLine(" File system: {0}", d.DriveFormat); Console.WriteLine( " Available space to current user:{0, 15} bytes", d.AvailableFreeSpace); Console.WriteLine( " Total available space: {0, 15} bytes", d.TotalFreeSpace); Console.WriteLine( " Total size of drive: {0, 15} bytes ", d.TotalSize); } } }}遍历目录类似。 private void Form1_Load(object sender, EventArgs e) { string[] pan = System.Environment.GetLogicalDrives(); foreach (string s in pan) { TreeNode node = new TreeNode(); if (s != "I:\\")//我得光驱 { node.Text = s; } else { return; } this.treeView1.Nodes.Add(node); string[] child = System.IO.Directory.GetDirectories(node.Text); foreach (string m in child) { TreeNode node1 = new TreeNode(); node1.Text = m.Substring(m.LastIndexOf("\\") + 1); node.Nodes.Add(node1); add_node(m,node1); } } this.treeView1.SelectedNode = this.treeView1.Nodes[0]; } void add_node(string pt,TreeNode nd) { string[] child = System.IO.Directory.GetDirectories(pt); foreach (string m in child) { TreeNode node1 = new TreeNode(); node1.Text = m.Substring(m.LastIndexOf("\\") + 1); nd.Nodes.Add(node1); add_node(m,node1); } } 枚举目录和文件用Directory下的GetDirectories和GetFiles 给个简单例子,遍历一层目录中有哪些文件夹和文件,至于遍历所有的文件夹和文件,只需要简单修改成递归就行:string filename = "D:/Music";DirectoryInfo d = new DirectoryInfo(filename);DirectoryInfo[] subd = d.GetDirectories();FileInfo [] subf = d.GetFiles();foreach(DirectoryInfo dd in subd){ Console.WriteLine(“文件夹:{0}",dd.ToString());} foreach(FileInfo ff in subf){ Console.WriteLine(“文件:{0}",ff.ToString());} static void Main(string[] args) { string[] drivers = Environment.GetLogicalDrives(); foreach (string driver in drivers) { Console.WriteLine(driver); DriveInfo driverinfo = new DriveInfo(driver); Console.WriteLine(driver + "'s freespace is " + (driverinfo.TotalFreeSpace/1024000).ToString()+"MB"); Console.WriteLine(driver + "'s usedspace is " + ((driverinfo.TotalSize - driverinfo.TotalFreeSpace)/1024000).ToString()+"MB"); } Console.ReadLine(); }你不要一次大开那么多啊,肯定会时间长的,你可以先打开驱动器,然后有人点击节点,在打开一级,一级一级的控制打开 请问四楼 if(s!="I:\\")//我得光驱 { node.Text = s; } else { return; } 这段代码是什么意思?我不是很清楚 怎么把textbox中输入的内容中的数字提取出来? C# 多线程 传参数 操作文件 报错 如何比较2个泛型集合中的值? string类中如何添加“这个符号? [100]如何选定webbrowser中id为某值的html对象? 新手,刚刚学习,刚刚来论坛,求C#附加数据库代码…… 同一个console只允许运行一个 怎么在C#里大开word 文档 如何将panel1中[启动程序后定义]的多个textbox中的数据存放到数组中。 请问如何在MDI窗体的主窗体上面画图? 如何访问BindingSource里的数据 一条面试题目,看似简单,但是无从下手!!
using System.IO;class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace); Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace); Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}
}遍历目录类似。
{
string[] pan = System.Environment.GetLogicalDrives();
foreach (string s in pan)
{
TreeNode node = new TreeNode();
if (s != "I:\\")//我得光驱
{
node.Text = s;
}
else
{
return;
}
this.treeView1.Nodes.Add(node);
string[] child = System.IO.Directory.GetDirectories(node.Text);
foreach (string m in child)
{
TreeNode node1 = new TreeNode();
node1.Text = m.Substring(m.LastIndexOf("\\") + 1);
node.Nodes.Add(node1);
add_node(m,node1);
} }
this.treeView1.SelectedNode = this.treeView1.Nodes[0];
} void add_node(string pt,TreeNode nd)
{
string[] child = System.IO.Directory.GetDirectories(pt);
foreach (string m in child)
{ TreeNode node1 = new TreeNode();
node1.Text = m.Substring(m.LastIndexOf("\\") + 1);
nd.Nodes.Add(node1);
add_node(m,node1);
}
}
DirectoryInfo d = new DirectoryInfo(filename);
DirectoryInfo[] subd = d.GetDirectories();
FileInfo [] subf = d.GetFiles();
foreach(DirectoryInfo dd in subd)
{
Console.WriteLine(“文件夹:{0}",dd.ToString());
}
foreach(FileInfo ff in subf)
{
Console.WriteLine(“文件:{0}",ff.ToString());
}
{
string[] drivers = Environment.GetLogicalDrives(); foreach (string driver in drivers)
{
Console.WriteLine(driver);
DriveInfo driverinfo = new DriveInfo(driver);
Console.WriteLine(driver + "'s freespace is " + (driverinfo.TotalFreeSpace/1024000).ToString()+"MB");
Console.WriteLine(driver + "'s usedspace is " + ((driverinfo.TotalSize - driverinfo.TotalFreeSpace)/1024000).ToString()+"MB");
} Console.ReadLine();
}你不要一次大开那么多啊,肯定会时间长的,你可以先打开驱动器,然后有人点击节点,在打开一级,一级一级的控制打开
if(s!="I:\\")//我得光驱
{
node.Text = s;
}
else
{
return;
}
这段代码是什么意思?我不是很清楚