C# 递归方法 加载 文件-----类似于 资源管理器

刚毕业,参加工作没多久,但是一直想写些  有关技术方面的文章,一来 ,为了 复习,二来,希望大家相互交流,相互指点,也希望 对初学者有所 帮助,

由于本人技术水平 有限,难免会出错,请见谅!

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

using System.IO;   //添加与文件有关的程序集

namespace FileList
{
public partial class Form1 : Form
{
protected string[] filePaths;   //声明 要加载的 文件路径 字符串//声明要 递归的文件的 路径数组
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
filePaths = new string[] { @"E:", @"F:",@"C:", @"D:" };  //实例化文件路径
foreach (string filePath in filePaths)
{
GetParent(filePath);  //遍历文件路径 并 获取 根节点
}
}
/// <summary>
/// 获取 根节点
/// </summary>
/// <param name="textNode"></param>
public void GetParent(string textNode)
{
TreeNode node1 = new TreeNode(textNode);
node1.Tag = textNode;
treeList.Nodes.Add(node1); //将根节点加入到 树形控件中
LoadTree(textNode, node1);//根据文件路径逐个 递归加载 所有的文件 


}

#region 加载目录下 所有子目录
/// <summary>
/// 加载目录下 所有子目录
/// </summary>
protected void LoadTree(string strPath, TreeNode node)
{
try
{

string[] strDirs = Directory.GetDirectories(strPath);//根据文件路径 加载里面的所有的 目录
foreach (string dir in strDirs)  //递归 目录数组
{
TreeNode node1 = new TreeNode(Path.GetFileName(dir)); //通过目录获取 文件名,并添加到 树形节点
node1.Tag = dir;// 将目录存入 node的Tag属性中
if (node == null)
{

treeList.Nodes.Add(node1);
}
else
{
node.Nodes.Add(node1);
}
if (Directory.GetDirectories(dir).Length > 0)
{
LoadTree(dir, node1);
}
}

}
catch (Exception ex)
{
}
#endregion


//选中节点之后 促发的 事件。
private void treeList_AfterSelect(object sender, TreeViewEventArgs e)
{
listFile.Items.Clear();
if (treeList.SelectedNode.Tag != null)
{
string selPath = treeList.SelectedNode.Tag.ToString();

string[] files = Directory.GetFiles(selPath);
ListViewItem item = null;
FileInfo infor = null;
foreach (string file in files)
{
infor = new FileInfo(file);
item = new ListViewItem();
item.Text = Path.GetFileName(file);
item.Tag = file;

item.SubItems.Add(infor.Length.ToString()+"KB");//大小
item.SubItems.Add(infor.CreationTime.ToString());//创建时间
string [] arr=infor.Name.Split('.');
item.SubItems.Add(arr[arr.Length-1].ToString());

listFile.Items.Add(item);

}
}


}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = listFile.SelectedItems[0].Tag.ToString();

if(MessageBox.Show("是否要删除?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==System.Windows.Forms.DialogResult.Yes)
{

File.Delete(path);
listFile.SelectedItems[0].Remove();
}
}

private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.listFile.FullRowSelect = false;
listFile.LabelEdit = true;
}

private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{

try
{
string paths = listFile.SelectedItems[0].Tag.ToString();
string[] type = paths.Split('.');
if (type[type.Length - 1].ToString() == "txt")
{

string path = listFile.SelectedItems[0].Tag.ToString();
FileStream stream = new FileStream(path, FileMode.Open);
byte[] buffer = new byte[1024 * 1024];
stream.Read(buffer, 0, buffer.Length);
string text = Encoding.Default.GetString(buffer);
ContentBox.Text = text;
stream.Dispose();
}
else
{
MessageBox.Show("目前只能打开txt文件");
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = listFile.SelectedItems[0].Tag.ToString();
FileStream stream = new FileStream(path, FileMode.Create);

string content = ContentBox.Text;
byte[] buffer = Encoding.Default.GetBytes(content);
stream.Write(buffer, 0, buffer.Length);
stream.Dispose();
MessageBox.Show("保存成功!");

}

  ///获取所有驱动盘目录
  string[] s = Directory.GetLogicalDrives();

原文地址:https://www.cnblogs.com/dlf-myDream/p/4292784.html