实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
using System.IO;
namespace MiniExplorer
{
//用TreeView实现文件浏览器
public partial class frmMiniExplorer : Form
{
private const int Removable = 2;
private const int LocalDisk = 3;
private const int Network = 4;
private const int CD = 5;
public frmMiniExplorer()
{
InitializeComponent();
}
private void frmMiniExplorer_Load(object sender, EventArgs e)
{
LoadDrives();
LoadListView();
}
//加载物理磁盘驱动器
private void LoadDrives()
{
TreeNode nodeTemp = null;
int imageIndex = 0;
int selectIndex = 0;
this.Cursor = Cursors.WaitCursor;
tvFolders.Nodes.Clear();
nodeTemp = new TreeNode("My Computer", 0, 0);
tvFolders.Nodes.Add(nodeTemp);//加载根目录
TreeNodeCollection nodeCollection = nodeTemp.Nodes;
//用System.Management对象获取驱动器列表
ManagementObjectCollection queryCollection = GetDriveList();
foreach (ManagementObject mo in queryCollection)
{
switch (int.Parse(mo["DriveType"].ToString()))
{
case Removable: //Removable Drives
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;
}
//建立驱动器
nodeTemp = new TreeNode(mo["Name"].ToString() "\\", imageIndex, selectIndex);
nodeCollection.Add(nodeTemp);
}
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)
LoadDrives();
else
LoadDirectory(nodeCurrent, nodeCurrent.Nodes);
this.Cursor = Cursors.Default;
}
protected void LoadListView()
{
lvFiles.Clear();
//创建ListView的标题列
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 LoadDirectory(TreeNode nodeCurrent, TreeNodeCollection nodeCurrentCollection)
{
TreeNode nodeTemp = null;
int imageIndex = 2; //当前结点的图片
int selectIndex = 3; //选中结点时显示的图片
if (nodeCurrent.SelectedImageIndex != 0)
{
try
{
if (Directory.Exists(FormatPath(nodeCurrent.FullPath)) == false)
throw new Exception("目录或路路径不存在!");
LoadFiles(nodeCurrent);
string[] stringDirectories = Directory.GetDirectories(FormatPath(nodeCurrent.FullPath));
string stringFullPath = "";
string stringPathName = "";
//加载当前目录下所有子目录
foreach (string stringDir in stringDirectories)
{
stringFullPath = stringDir;
stringPathName = GetPathName(stringFullPath);
TreeNode nodeDir = new TreeNode(stringPathName.ToString(), imageIndex, selectIndex);
nodeCurrentCollection.Add(nodeDir);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
protected string GetPathName(string stringPath)
{
string[] stringSplit = stringPath.Split(’\\’);
return stringSplit[stringSplit.Length - 1];
}
//加载当前目录下的文件
protected void LoadFiles(TreeNode nodeCurrent)
{
string[] listviewData = new string[4];
this.LoadListView();
if (nodeCurrent.SelectedImageIndex != 0)
{
if (Directory.Exists((string)FormatPath(nodeCurrent.FullPath)) == false)
throw new Exception("目录或路路径不存在!");
try
{
string[] stringFiles = Directory.GetFiles(FormatPath(nodeCurrent.FullPath));
DateTime dtCreateDate, dtModifyDate;
Int64 lFileSize = 0;
foreach (string stringFile in stringFiles)
{
FileInfo objFileSize = new FileInfo(stringFile);
lFileSize = objFileSize.Length;
dtCreateDate = objFileSize.CreationTime;
dtModifyDate = objFileSize.LastWriteTime;
listviewData[0] = GetPathName(stringFile);
listviewData[1] = FormatSize(lFileSize);
if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
listviewData[2] = FormatDate(dtCreateDate.AddHours(1));
else
listviewData[2] = FormatDate(dtCreateDate);
if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
listviewData[3] = FormatDate(dtModifyDate.AddHours(1));
else
listviewData[3] = FormatDate(dtModifyDate);
ListViewItem lvItem = new ListViewItem(listviewData, 0);
lvFiles.Items.Add(lvItem);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
//替代Root是My Computer
protected string FormatPath(string stringPath)
{
string stringParse = stringPath.Replace("My Computer\\", "");
return stringParse;
}
//获取所有磁盘驱器
protected ManagementObjectCollection GetDriveList()
{
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;
}
//将文件大小转换为带KB的字符串
protected string FormatSize(Int64 lSize)
{
string stringSize = "";
NumberFormatInfo numInfo = new NumberFormatInfo();
if (lSize < 1024) //小于1KB
{
if (lSize == 0)
stringSize = "0";
else
stringSize = "1";
}
else
{
Int64 lKBSize = lSize / 1024; //转折为MB / 1024
stringSize = lKBSize.ToString("n", numInfo);
stringSize = stringSize.Replace(".00", "");
}
return stringSize " KB";
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)