首先可以使用managementobjectsearcher类来查询system.management域,来获取返回managementojbectcollection,在这个对象中,包含了我们所需要的信息,包括盘,文件夹,文件,现在让我们来看一下整个源码:
system.windows.forms.splitter splitter1;
private system.windows.forms.mainmenu mainmenu1;
private system.windows.forms.menuitem menuitem1;
private system.windows.forms.menuitem menuitem2;
private system.windows.forms.menuitem menuitem3;
private system.windows.forms.menuitem menuitem4;
private system.windows.forms.treeview tvfolders;
private system.windows.forms.listview lvfiles;
private system.windows.forms.imagelist m_imagelisttreeview;
private system.componentmodel.icontainer components;
///
这里是ide自动产生的
///
private void populatedrivelist()
{
treenode nodetreenode;
int imageindex = 0;
int selectindex = 0;
const int removable = 2;
const int localdisk = 3;
const int network = 4;
const int cd = 5;
this.cursor = cursors.waitcursor;
tvfolders.nodes.clear();//清空树
nodetreenode = new treenode("my computer",0,0);//
tvfolders.nodes.add(nodetreenode);
treenodecollection nodecollection = nodetreenode.nodes;
managementobjectcollection querycollection = getdrives();
foreach ( managementobject mo in querycollection)
{
switch (int.parse( mo["drivetype"].tostring()))
{
case removable: //可移动盘
imageindex = 5;
selectindex = 5;
break;
case localdisk: //本地磁盘
imageindex = 6;
selectindex = 6;
break;
case cd: //光盘
imageindex = 7;
selectindex = 7;
break;
case network: //网络驱动盘
imageindex = 8;
selectindex = 8;
break;
default: //默认
imageindex = 2;
selectindex = 3;
break;
}
//建立驱动盘的节点
nodetreenode = new treenode(mo["name"].tostring() + "\\" ,imageindex,selectindex);
nodecollection.add(nodetreenode);
}
//init files listview
initlistview();
this.cursor = cursors.default;
}
private void tvfolders_afterselect(object sender, system.windows.forms.treevieweventargs e)
{
this.cursor = cursors.waitcursor;
//获取当前选择的节点或则文件夹
treenode nodecurrent = e.node;
//清除掉所有的节点
nodecurrent.nodes.clear();
if (nodecurrent.selectedimageindex == 0)
{
populatedrivelist();
}
else
{
populatedirectory(nodecurrent, nodecurrent.nodes);
}
this.cursor = cursors.default;
}
protected void initlistview()
{
//开始初始化 listview 控件
lvfiles.clear();
//为察看列表框建立头信息栏
lvfiles.columns.add("name",150,system.windows.forms.horizontalalignment.left);
lvfiles.columns.add("size",75, system.windows.forms.horizontalalignment.right);
lvfiles.columns.add("created", 140, system.windows.forms.horizontalalignment.left);
lvfiles.columns.add("modified", 140, system.windows.forms.horizontalalignment.left);
}
protected void populatedirectory(treenode nodecurrent, treenodecollection nodecurrentcollection)
{
treenode nodedir;
int imageindex = 2;
int selectindex = 3;
if (nodecurrent.selectedimageindex != 0)
{
try
{
if(directory.exists(getfullpath(nodecurrent.fullpath)) == false)
{
messagebox.show("directory or path " + nodecurrent.tostring() + " does not exist.");
}
else
{
populatefiles(nodecurrent);
string[] stringdirectories = directory.getdirectories(getfullpath(nodecurrent.fullpath));
string stringfullpath = "";
string stringpathname = "";
//循环搜索整个目录
foreach (string stringdir in stringdirectories)
{
stringfullpath = stringdir;
stringpathname = getpathname(stringfullpath);
//创建目录节点
nodedir = new treenode(stringpathname.tostring(),imageindex,selectindex);
nodecurrentcollection.add(nodedir);
}
}
}
catch (ioexception e)
{
messagebox.show("error: drive not ready or directory does not exist.");
}
catch (unauthorizedaccessexception e)
{
messagebox.show("error: drive or directory access denided.");
}
catch (exception e)
{
messagebox.show("error: " + e);
}
}
}
protected string getpathname(string stringpath)
{
//得到文件的数目
string[] stringsplit = stringpath.split(\\);
int _maxindex = stringsplit.length;
return stringsplit[_maxindex-1];
}
protected void populatefiles(treenode nodecurrent)
{
string[] lvdata = new string[4];
initlistview();
if (nodecurrent.selectedimageindex != 0)
{
//检查路径
if(directory.exists((string) getfullpath(nodecurrent.fullpath)) == false)
{
messagebox.show("directory or path " + nodecurrent.tostring() + " does not exist.");
}
else
{
try
{
string[] stringfiles = directory.getfiles(getfullpath(nodecurrent.fullpath));
string stringfilename = "";
datetime dtcreatedate, dtmodifydate;
int64 lfilesize = 0;
foreach (string stringfile in stringfiles)
{
stringfilename = stringfile;
fileinfo objfilesize = new fileinfo(stringfilename);
lfilesize = objfilesize.length;
dtcreatedate = objfilesize.creationtime; //得到建立文件时候的时间;
dtmodifydate = objfilesize.lastwritetime; //得到最后修改文件时候的时间;
lvdata[0] = getpathname(stringfilename);
lvdata[1] = formatsize(lfilesize);
if (timezone.currenttimezone.isdaylightsavingtime(dtcreatedate) == false)
{
lvdata[2] = formatdate(dtcreatedate.addhours(1));
}
else
{
lvdata[2] = formatdate(dtcreatedate);
}
if (timezone.currenttimezone.isdaylightsavingtime(dtmodifydate) == false)
{
lvdata[3] = formatdate(dtmodifydate.addhours(1));
}
else
{
lvdata[3] = formatdate(dtmodifydate);
}
listviewitem lvitem = new listviewitem(lvdata,0);
lvfiles.items.add(lvitem);
}
}
catch (ioexception e)
{
messagebox.show("error: drive not ready or directory does not exist.");
}
catch (unauthorizedaccessexception e)
{
messagebox.show("error: drive or directory access denided.");
}
catch (exception e)
{
messagebox.show("error: " + e);
}
}
}
}
protected string getfullpath(string stringpath)
{
//得到完整的路径
string stringparse = "";
//移除掉“my computer"名称
stringparse = stringpath.replace("my computer\\", "");
return stringparse;
}
protected managementobjectcollection getdrives()
{
//收集驱动
managementobjectsearcher query = new managementobjectsearcher("select * from win32_logicaldisk ");
managementobjectcollection querycollection = query.get();
return querycollection;
}
protected string formatdate(datetime dtdate)
{
//以短格式得到日期和时间
string stringdate = "";
stringdate = dtdate.toshortdatestring().tostring() + " " + dtdate.toshorttimestring().tostring();
return stringdate;
}
protected string formatsize(int64 lsize)
{
string stringsize = "";
numberformatinfo mynfi = new numberformatinfo();//根据区域定义如何设置数字格式和如何显示数字格式
int64 lkbsize = 0;
if (lsize < 1024 )
{
if (lsize == 0)
{
stringsize = "0";
}
else
{
stringsize = "1";
}
}
else
{
lkbsize = lsize / 1024;
stringsize = lkbsize.tostring("n",mynfi);
//移除掉浮点数
stringsize = stringsize.replace(".00", "");
}
return stringsize + " kb";
}
private void menuitem2_click(object sender, system.eventargs e)
{
//退出应用程序
this.close();
}
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


