递归改进(在左侧树形不仅可以显示目录还可以显示跟要查询子目录统计的所有文件)
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;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string path = @"f:\腾讯游戏\地下城与勇士";
private void Form2_Load(object sender, EventArgs e)
{
Assembly ass = this.GetType().Assembly;
Type dog = typeof(dog);
PropertyInfo[] pri = dog.GetProperties();
foreach (PropertyInfo pi in pri )
{
MessageBox.Show("pri "+pi.ToString());
}
LoadTree(null, path);
string str1 = "abcdefghi";
string strq = "def";
Console.WriteLine( ceshi(str1,strq));
}
private string ceshi(string str1,string strq)
{
if (strq.Length > str1.Length)
return "zhaobudao";
;
for (int i = 0; i < str1.Length - strq.Length - 1; i++)
{
System.Text.StringBuilder sb = new StringBuilder();
if( str1.Substring(i, strq.Length).Equals(strq)) return "zhaodaole"+(i+1);
} return "9";
}
public static int i = 0;
private void LoadTree(TreeNode tnode,string path)
{
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
i++;
TreeNode tr = new TreeNode();
tr.Tag = dir;
tr.Text = Path.GetFileName(dir);
tr.Name = tr.Text;
if (tnode == null)
{
if (tr.Text == "SoundPacks")
{
MessageBox.Show(tr.Text);
}
treeView1.Nodes.Add(tr);
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
i++;
FileInfo f = new FileInfo(file);
TreeNode td = new TreeNode();
td.Tag = file;
td.Text = Path.GetFileName(file);
treeView1.Nodes.Add(td);
}
}
else
{ tnode.Nodes.Add(tr); }
string[] dirss = Directory.GetDirectories(dir);
if (dirss != null)
{
LoadTree(tr, dir);
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string path = (treeView1.SelectedNode.Tag.ToString());
try
{
string[] files = Directory.GetFiles(path);
listView1.Items.Clear();
foreach (string file in files)
{
FileInfo f = new FileInfo(file);
ListViewItem item = new ListViewItem();
item.Tag = file;
item.Text = Path.GetFileName(file);
item.SubItems.Add(f.Length.ToString());
item.SubItems.Add(f.CreationTime.ToString());
listView1.Items.Add(item);
}
}
catch (Exception ex)
{
;
}
}
public int index;
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// string path =listView1. SelectedItems[0].Tag.ToString();
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
if (listView1.SelectedItems[i].Selected)
{
index = i;
// MessageBox.Show("ni xuan zhong le"+Path.GetFileName(listView1.SelectedItems[i].Tag.ToString()));
}
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("你真的要删除吗?删除以后就找不到了哦!", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes&&index>=0)
listView1.Items.RemoveAt(index);
}
catch (Exception)
{
MessageBox.Show("目录已经为空");
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void alterToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}