private void button2_Click(object sender, EventArgs e)
{
DirDG(treeView1.Nodes.Add(@"F:/我的项目", "我的项目"));
}
//以节点的形式获取的所有目录文件
void DirDG(TreeNode tn)
{
try
{
foreach (string str in Directory.GetDirectories(tn.Name))
{
DirDG(tn.Nodes.Add(str, Path.GetFileNameWithoutExtension(str)));
}
}
catch
{ }
}
private void button3_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
//遍历节点下的文件得到名称添加到ListViewItem 中
foreach (string str in Directory.GetDirectories
(treeView1.SelectedNode.Name))
{
ListViewItem lvi = listView1.Items.Add(str,
Path.GetFileNameWithoutExtension(str), 0);
//获得详细信息的列表
lvi.SubItems.Add(Directory.GetLastWriteTime(str).ToString("yyyy年MM
月dd日"));
lvi.SubItems.Add("文件夹");
lvi.SubItems.Add(GetSize(str).ToString());
}
//得到所有的文件
foreach (string str in Directory.GetFiles(treeView1.SelectedNode.Name))
{
ListViewItem lvi = listView1.Items.Add(str, Path.GetFileName(str),
0);
lvi.SubItems.Add(File.GetLastWriteTime(str).ToString("yyyy年MM月dd日
"));
lvi.SubItems.Add(Path.GetExtension(str));
lvi.SubItems.Add(new FileInfo(str).Length.ToString());
}
}
//获取文件的大小
long GetSize(string path)
{
long len = 0;
foreach (string str in Directory.GetDirectories(path))
{
len = GetSize(str);
}
long Len = 0;
foreach (string str in Directory.GetFiles(path))
{
FileInfo fi = new FileInfo(str);
Len += fi.Length;
}
return Len + len;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.View = (View)Enum.Parse(typeof(View),
comboBox1.SelectedItem.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = Enum.GetNames(typeof(View));
}
private void listView1_Click(object sender, EventArgs e)
{
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
string path = listView1.SelectedItems[0].Name;
if (!Path.HasExtension(path))
{
listView1.Items.Clear();
foreach (string str in Directory.GetDirectories(path))
{
ListViewItem lvi = listView1.Items.Add(str,
Path.GetFileNameWithoutExtension(str), 0);
lvi.SubItems.Add(Directory.GetLastWriteTime(str).ToString("yyyy
年MM月dd日"));
lvi.SubItems.Add("文件夹");
lvi.SubItems.Add(GetSize(str).ToString());
}
}
else
{//带扩展名的直接打开文件
Process.Start(path);
}
}