实例介绍
【实例简介】
【实例截图】
【核心代码】
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 FileExplorer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 定义初始的全局变量
/// </summary>
string explorerPath = "";
string treeViewPath = "";
/// <summary>
/// 点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_open_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string path = folderBrowserDialog.SelectedPath;
explorerPath = path;
treeViewPath = path;
this.textBox_path.Text = path;
this.treeView_list.Nodes.Clear();
getExplorerView(null, path);
}
}
/// <summary>
/// 单层遍历,仅显示当前目录下的文件夹和文件
/// </summary>
/// <param name="path"></param>
private void getFolderView(string path)
{
explorerPath = path;
textBox_path.Text = explorerPath;
try
{
this.listView_show.Items.Clear();
DirectoryInfo TheFolder = new DirectoryInfo(path);
//遍历文件夹
foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
{
this.listView_show.Items.Add("【" NextFolder.Name "】");
}
//遍历文件
foreach (FileInfo NextFile in TheFolder.GetFiles())
this.listView_show.Items.Add(NextFile.Name); //再添加文件大小显示
}
catch(Exception)
{
}
}
/**
* 遍历方案: -- 深度优先遍历
* 1.获取当前目录下的所有文件夹
* 2.将第一个目录添加到treeView节点,并返回该节点的 TreeNode对象
* 3.递归将返回到treeNode对象传递进去,还有子级目录的文件夹名称
* 4.循环
* 5.深度结束条件:子级目录为空
* 问题,第一层根节点怎么传值? 解决 传一个 null
*/
/// <summary>
/// 遍历函数
/// </summary>
/// <param name="path"></param>
private void getExplorerView(TreeNode node,string path)
{
try
{
TreeNode newnode = null;
DirectoryInfo TheFolder = new DirectoryInfo(path);
//遍历文件夹
foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
{
newnode = addTreeViewLevelNode(node, NextFolder.Name); //添加一个节点
getExplorerView(newnode, NextFolder.FullName);
}
}
catch (Exception)
{
MessageBox.Show("路径不存在");
}
}
/// <summary>
/// 目录遍历函数
/// </summary>
/// <param name="node"></param>
/// <param name="name"></param>
/// <returns></returns>
private TreeNode addTreeViewLevelNode(TreeNode node, string name)
{
TreeNode levelNode = new TreeNode();
levelNode.Text = name;
if (node == null)
{
treeView_list.Nodes.Add(levelNode);
}
else
{
levelNode.Text = name;
node.Nodes.Add(levelNode);
}
return levelNode;
}
/// <summary>
/// treeView节点点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_list_AfterSelect(object sender, TreeViewEventArgs e)
{
string newPath = "";
TreeNode node = null;
try
{
node = e.Node.Parent;
newPath = e.Node.Text;
while (true)
{
newPath = newPath.Insert(0, node.Text "\\"); //node.Text "\\"
node = node.Parent;
}
}
catch (NullReferenceException)
{
}
if (!newPath.Equals(""))
{
explorerPath = newPath.Insert(0, treeViewPath "\\");
getFolderView(explorerPath);//遍历该层文件
}
}
/// <summary>
/// 处理listView中目录点击展开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView_show_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
string filename = this.listView_show.SelectedItems[0].SubItems[0].Text;
if (filename.StartsWith("【")) //目录处理方式
{
filename = filename.Replace("【", "");
filename = filename.Replace("】", "");
string newPath = explorerPath "\\" filename;
getFolderView(newPath);//遍历该层文件
}
else //文件处理方式
{
string newPath = explorerPath "\\" filename;
//显示文件大小
int size = getFileSize(newPath);
if(size > 1024*1024)
{
toolStripStatusLabel3.Text = ((float)size / (1024 * 1024)).ToString("F1") "GB";
}
else if (size>1024)
{
toolStripStatusLabel3.Text = ((float)size / 1024).ToString("F1") "MB";
}
else
{
toolStripStatusLabel3.Text = size "KB";
}
OpenFile(newPath);
}
}
catch (ArgumentOutOfRangeException)
{
//MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 后退按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_back_Click(object sender, EventArgs e)
{
try
{
string newPath = explorerPath.Substring(0, explorerPath.LastIndexOf("\\"));
getFolderView(newPath);
}catch(ArgumentOutOfRangeException)
{
}
}
/// <summary>
/// 打开文件,仅支持文本文件,而且文件大小小于1MB
/// </summary>
/// <param name="path"></param>
private void OpenFile(string path)
{
if(getFileSize(path)<1000 && getFileType(path)) //如果文件小于1000KB,并且后缀为文本文件类型
{
Read_line(path);
}
else if (getFileSize(path) < 5000 && getPicFileType(path))
{
openPic(path);
}
}
/// <summary>
/// 读取文本文件 - 自动换行
/// </summary>
/// <param name="path"></param>
public void Read_line(string path)
{
this.richTextBox_txtShow.Visible = true;
this.pictureBox1.Visible = false;
this.richTextBox_txtShow.Clear();
StreamReader sr = new StreamReader(path, Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
this.richTextBox_txtShow.Text = line.ToString() "\n";
}
sr.Close();
}
/// <summary>
/// 打开一幅图像,前提是先将之前的文本控件隐藏,然后显示图片控件
/// </summary>
/// <param name="path"></param>
private void openPic(string path)
{
this.richTextBox_txtShow.Visible = false;
this.pictureBox1.Visible = true;
this.pictureBox1.ImageLocation = path;
}
/// <summary>
/// 获取文件的大小
/// </summary>
/// <param name="path"></param>
private int getFileSize(string path)
{
try
{
FileInfo fi = new FileInfo(path); //返回的是字节大小
float x = ((float)fi.Length / 1024);
int result = (int)(x 1);
return result;
}
catch (FileNotFoundException)
{
return 0;
}
}
/// <summary>
/// 获取文件类型是否是文本类型,主要是判断文件后缀。
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private bool getFileType(string path)
{
string fileType = path.Substring(path.LastIndexOf(".") 1, path.Length - path.LastIndexOf(".") -1);
string[] type = { "txt", "xml", "ini", "conf", "java", "cs", "sql", "html", "js", "css", "c", "h", "cpp","py" };
for (int i = 0; i < type.Length;i )
{
if (fileType.Equals(type[i]))
{
return true;
}
}
return false;
}
/// <summary>
/// 获取文件类型是否为图片,主要是根据文件后缀判断
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private bool getPicFileType(string path)
{
string fileType = path.Substring(path.LastIndexOf(".") 1, path.Length - path.LastIndexOf(".") - 1);
string[] type = { "jpg", "jpeg", "png", "bmp", "ico", "gif" };
for (int i = 0; i < type.Length; i )
{
if (fileType.Equals(type[i]))
{
return true;
}
}
return false;
}
/// <summary>
/// 定时器-状态栏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StatuUpdate(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToString();//时间
toolStripStatusLabel2.Text = "共" this.listView_show.Items.Count "个项目"; //文件数
}
/// <summary>
/// 转到
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_go_Click(object sender, EventArgs e)
{
string path = this.textBox_path.Text;
explorerPath = path;
treeViewPath = path;
this.treeView_list.Nodes.Clear();
getExplorerView(null, path);
}
/// <summary>
/// 文件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_delete_Click(object sender, EventArgs e)
{
try
{
string filename = this.listView_show.SelectedItems[0].SubItems[0].Text;
string newPath = explorerPath "\\" filename;
if (!filename.Equals(""))
{
DialogResult r1 = MessageBox.Show("是否永久删除该文件?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (r1.ToString() == "Yes")
{
//删除文件
File.Delete(newPath);
getFolderView(explorerPath);
}
else if (r1.ToString().Equals("No"))
{
return;
}
}
}
catch (Exception)
{
MessageBox.Show("暂时不允许删除目录");
}
}
/// <summary>
/// 拷贝按钮点击事件,目前仅针对文件,文件夹不可以
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
string copy_path = "";
string copy_name = "";
private void button_copy_Click(object sender, EventArgs e)
{
this.button_parse.Enabled = true;
copy_name = this.listView_show.SelectedItems[0].SubItems[0].Text;
copy_path = explorerPath "\\" copy_name;
}
/// <summary>
/// 剪切按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
string cut_path = "";
string cut_name = "";
private void button_cut_Click(object sender, EventArgs e)
{
this.button_parse.Enabled = true;
cut_name = this.listView_show.SelectedItems[0].SubItems[0].Text;
cut_path = explorerPath "\\" cut_name;
}
/// <summary>
/// 粘贴按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
string parse_path = "";
private void button_parse_Click(object sender, EventArgs e)
{
try
{
parse_path = explorerPath;
if (!copy_path.Equals(""))
{
File.Copy(copy_path, parse_path "\\" copy_name);
getFolderView(parse_path);
copy_path = "";
}
else if (!cut_path.Equals(""))
{
File.Move(cut_path, parse_path "\\" cut_name);
getFolderView(parse_path);
cut_path = "";
}
}catch(Exception)
{
MessageBox.Show("目前支持对文件操作");
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论