电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
建立自己的资源管理器-.NET教程,评论及其它
作者:网友供稿 点击:8
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
这篇文章介绍了如何得到本地系统信息,通过使用system.management和sysetm.io 来得到文件夹和文件信息,并且把它们显示到treeview、listview控件z之中。

首先可以使用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
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号