您的位置:首页 > 运动休闲 > 旅游用品 > Windows资源管理器

Windows资源管理器

luyued 发布于 2011-02-26 18:34   浏览 N 次  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LdProgram
{
public partial class Form1 : Form
{

TreeNode CurrentNode = null;//当前节点
string FileName = "新建文件夹";//新建文件夹名称
string SoursePathName = "";//源路径
string SourseFileName = "";//源文件夹名
string DestPathName = "";//粘贴路径
LdProgram.Comon.ComonOperat comonOperat = new LdProgram.Comon.ComonOperat(); //实例化文件操作对象

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

string[] sarray = Directory.GetLogicalDrives();//获取磁盘目录
foreach (string item in sarray)
{
TreeNode node = new TreeNode(item);
node.Text = item;
node.Tag = item;
GetDirectors(node);//调用获取磁盘子目录方法
tvlist.Nodes.Add(node);
}
lvlist.Columns.Add("文件名", 100, HorizontalAlignment.Center);
lvlist.Columns.Add("文件创建时间", 100, HorizontalAlignment.Center);
lvlist.Columns.Add("文件路径", 150, HorizontalAlignment.Center);
}
//TreeView展开之前事件
private void tvlist_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
tvlist.BeginUpdate();
foreach (TreeNode item in e.Node.Nodes)
{
GetDirectors(item);//调用添加磁盘子目录方法
}
AddLiseView(e.Node);//添加ListView节点
tvlist.EndUpdate();
}

//TreeView节点选中之后一些空间可以使用

private void tvlist_AfterSelect_1(object sender, TreeViewEventArgs e)
{
e.Node.Expand();//节点展开
CurrentNode = e.Node;
if (e.Node.Level > 0)
{
mspCreateDirFile.Enabled = true;
mspDeleteDirFile.Enabled = true;
mspRenameDirFile.Enabled = true;
tspCreateDirFile.Enabled = true;
tspDeleteDirFile.Enabled = true;
cmsCreatDirFile.Enabled = true;
cmsDeleteDirFile.Enabled = true;
cmsRenameDieFile.Enabled = true;
tspPastFile.Enabled = true;
tspCopyFile.Enabled = true;
mspCopyFile.Enabled = true;
mspDeleteFile.Enabled = true;
}
}
//AfterLableEdit----实际路径里添加文件夹
private void tvlist_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
string tmp = "";
if (e.Label == null)
tmp = e.Node.Text;
else
tmp = e.Label;
if (!comonOperat.CreatDirFile(Path.Combine(e.Node.Parent.Tag.ToString(), tmp)))
{
e.Node.Remove();//如果创建不成功就删除这个节点
}
e.Node.Tag = Path.Combine(e.Node.Parent.Tag.ToString(), tmp);
CurrentNode = e.Node;
tvlist.LabelEdit = false;
}

//小图标
private void mspSmallIcon_Click(object sender, EventArgs e)
{
lvlist.View = View.SmallIcon;
}

//列表

private void mspLink_Click(object sender, EventArgs e)
{
lvlist.View = View.List;
}
private void lvlist_SelectedIndexChanged(object sender, EventArgs e)
{
cmsrCopyFile.Enabled = true;
cmsrDeletFile.Enabled = true;
cmsrRenameFile.Enabled = true;
}

#region 文件操作
private void tspAbout_Click(object sender, EventArgs e)
{
MessageBox.Show("MiniExplorer V1.0 Powered By Aptech", "关于", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void tspOut_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void cmsDeleteDirFile_Click(object sender, EventArgs e)//删除文件夹
{
if (CurrentNode == null) return;
if (Directory.Exists(CurrentNode.Tag.ToString()))
{
comonOperat.DeletDirFile(CurrentNode.Tag.ToString());
CurrentNode.Remove();


}
}
private void tspCopyFile_Click(object sender, EventArgs e)//复制文件
{
//把完整文件保存到一个变量里(源路径),目标路径-鼠标选中的路径(判断是否为文件夹?)
//目标文件路径+要复制的文件名
//获取文件路径
SoursePathName = CurrentNode.Tag.ToString();//原路径
SourseFileName = CurrentNode.Text;//要复制的文件名
}

private void tspPastFile_Click(object sender, EventArgs e)//粘贴文件
{
DestPathName = CurrentNode.Tag.ToString();//复制到的文件路径
if (Directory.Exists(DestPathName))
{


comonOperat.CopyDirFile(SoursePathName, Path.Combine(DestPathName, SourseFileName));
CurrentNode.Nodes.Add(SourseFileName);//添加新节点(新节点的名字是要复制的文件名)
//如果是移动:实际路径里删除原文件,TreeNode上要删除原节点
// CurrentNode.Remove();
}
}
}

private void tspRenameDirFile_Click(object sender, EventArgs e)//重命名文件夹
{
tvlist.LabelEdit = true;//设置TreeView节点可编辑
CurrentNode.BeginEdit();//开始编辑选中的节点
}
private void tspCreatDieFile_Click_1(object sender, EventArgs e)//新建文件夹------添加节点
{
if (CurrentNode == null) return;
TreeNode node = new TreeNode(FileName);//添加一个节点,名字是要新建的文件夹的名字
node.Tag = Path.Combine(CurrentNode.Tag.ToString(), FileName);//新建节点的路径就是其父节点(当前选中节点)+文件夹名
CurrentNode.Nodes.Add(node);
tvlist.LabelEdit = true;//设定当前节点可编辑
node.BeginEdit();
}

//关闭到托盘
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//e.Cancel = true;
//this.Hide();
//this.ShowInTaskbar = false;
//this.noSmall.Visible = true;
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Show();
this.ShowInTaskbar = true;
this.noSmall.Visible = false;
}
}


#endregion

#region 自定义方法
///


/// 添加TreeView的子目录
///

/// 子节点
private void GetDirectors(TreeNode nodes)
{
nodes.Nodes.Clear();
DirectoryInfo difo = new DirectoryInfo(nodes.Tag.ToString());
DirectoryInfo[] difoarray;
try { difoarray = difo.GetDirectories(); }
catch { return; }
foreach (DirectoryInfo dir in difoarray)
{
TreeNode childnode = new TreeNode(dir.Name);
childnode.Tag = dir.FullName;
childnode.Text = dir.Name;
nodes.Nodes.Add(childnode);
}
//遍历文件
//FileInfo[] filearray = difo.GetFiles();
//foreach (FileInfo fitem in filearray)
//{
// TreeNode childnode = new TreeNode(fitem.Name);
// childnode.Tag = fitem.FullName;
// childnode.Text = fitem.Name;
// nodes.Nodes.Add(childnode);
//}
}

///


/// 添加treeview相对应的listview节点,以treenode节点为参数
///

///
private void AddLiseView(TreeNode listnode)
{
lvlist.Items.Clear();
DirectoryInfo dirinfo = new DirectoryInfo(listnode.Tag.ToString());
//遍历文件
DirectoryInfo[] dirinfos = dirinfo.GetDirectories();
foreach (DirectoryInfo ditem in dirinfos)
{
ListViewItem lvlistitem = new ListViewItem(ditem.Name);
lvlistitem.SubItems.Add(ditem.CreationTime.ToString("yyyy-MM-dd"));
lvlistitem.SubItems.Add(ditem.FullName);
lvlist.Items.Add(lvlistitem);
PathListView = ditem.FullName;
}
//遍历文件
FileInfo[] filearray = dirinfo.GetFiles();
foreach (FileInfo fitem in filearray)
{
ListViewItem lvlistitem = new ListViewItem(fitem.Name);
lvlistitem.SubItems.Add(fitem.CreationTime.ToString("yyyy-MM-dd"));
lvlistitem.SubItems.Add(fitem.FullName);
lvlist.Items.Add(lvlistitem);
}
}
#endregion

private void lvlist_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
CurrentItem=e.Item;
cmsrCopyFile.Enabled = true;
cmsrDeletFile.Enabled = true;
cmsrRenameFile.Enabled = true;
}
}
}


MSN空间完美搬家到新浪博客!

图文资讯
广告赞助商