public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TreeView treeView = null;
ListView listView = null;
private void Form1_Load(object sender, EventArgs e)
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.ColumnCount = 2;
tableLayoutPanel.RowCount = 2;
tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
//tableLayoutPanel.BackColor = Color.Red;
this.Controls.Add(tableLayoutPanel);
Label label = new Label();
label.Text = "Hello!!";
label.BackColor = Color.Blue;
label.Font = new Font("黑体", 20);
label.Size = new Size(this.Width, 50);
label.TextAlign = ContentAlignment.MiddleCenter;
tableLayoutPanel.Controls.Add(label, 0, 0);
label.Dock = DockStyle.Fill;
tableLayoutPanel.SetColumnSpan(label, 2);
treeView = new TreeView();
treeView.Name = "treeView";
treeView.Dock = DockStyle.Fill;
CreateParent();
tableLayoutPanel.Controls.Add(treeView, 0, 1);
listView = new ListView();
listView.Name = "listView";
ListViewItem item = new ListViewItem();
ColumnHeader cName = new ColumnHeader();
cName.Text = "名称";
ColumnHeader size = new ColumnHeader();
size.Text = "大小";
ColumnHeader time = new ColumnHeader();
time.Text = "日期";
listView.Columns.Add(cName);
listView.Columns.Add(size);
listView.Columns.Add(time);
listView.View = View.Details;
listView.Dock = DockStyle.Fill;
tableLayoutPanel.Controls.Add(listView, 1, 1);
listView.FullRowSelect = true;
listView.ItemActivate += new EventHandler(listView_ItemActivate);
this.WindowState = FormWindowState.Maximized;
}
void listView_ItemActivate(object sender, EventArgs e)
{
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
bool b = lw.SelectedItems[0].Text.Contains('.');
string filename = lw.SelectedItems[0].Tag.ToString();
if (b)
{
try
{
System.Diagnostics.Process.Start(filename);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
CreateListView(filename);
}
}
void CreateParent()
{
treeView.Nodes.Clear();
string path = @"D:\";
DirectoryInfo di = new DirectoryInfo(path);
if (!di.Exists)
{
di.Create();
}
TreeNode parent = new TreeNode();
parent.Text = di.Name;
parent.Tag = di.FullName;
treeView.Nodes.Add(parent);
CreateChild(parent);
treeView.Size = new Size(400, this.Height);
treeView.ExpandAll();
treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
treeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView_NodeMouseClick);
}
//void listView_DoubleClick(object sender, EventArgs e)
//{
// string itemname = listView.SelectedItems[0].Text;
// DirectoryInfo di = new DirectoryInfo(path);
// DirectoryInfo[] dis = di.GetDirectories();
// foreach (DirectoryInfo diinfo in dis)
// {
// listView.Items.Add(diinfo.Name);
// }
//}
void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
listView.Items.Clear();
}
void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
string path = e.Node.Tag.ToString();
CreateListView(path);
//listView.DoubleClick += new EventHandler(listView_DoubleClick);
//}
}
public void CreateListView(string path)
{
listView.Items.Clear();
DirectoryInfo di = new DirectoryInfo(path);
//if (di.Extension == "")
//{
FileInfo[] files = di.GetFiles();
DirectoryInfo[] dis = di.GetDirectories();
foreach (FileInfo file in files)
{
ListViewItem item = new ListViewItem(new String[] { file.Name, file.Length.ToString(), file.LastAccessTime.ToString() });
listView.Items.Add(item);
item.Tag = file.FullName;
//listView.Columns["cName"].Text=file.Name;
//listView.Items.Add(file.Name);
}
foreach (DirectoryInfo diinfo in dis)
{
string size = FileSize(diinfo.FullName).ToString();
ListViewItem item = new ListViewItem(new String[] { diinfo.Name, size, diinfo.LastAccessTime.ToString() });
listView.Items.Add(item);
item.Tag = diinfo.FullName;
//listView.Items.Add(diinfo.Name);
}
}
public static long FileSize(string filePath)
{
long temp = 0;
//if (!File.Exists(filePath) )
//{
// string[] str1 = Directory.GetFileSystemEntries(filePath);
// foreach (string s1 in str1)
// {
// temp += FileSize(s1);
// }
//}
//else
//{
// FileInfo fileInfo = new FileInfo(filePath);
// return fileInfo.Length;
//}
string[] arrFile = Directory.GetFiles(filePath, "*.*", SearchOption.AllDirectories);
FileInfo fi = null;
foreach (var item in arrFile)
{
fi = new FileInfo(item);
temp += fi.Length;
}
return temp;
}
void CreateChild(TreeNode parent)
{
string path = parent.Tag.ToString();
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*");
foreach (FileInfo file in files)
{
if (file.Extension == "")
{
TreeNode child = new TreeNode();
child.Text = Path.GetFileName(file.Name);
child.Tag = file.FullName;
parent.Nodes.Add(child);
}
}
DirectoryInfo[] dirs = di.GetDirectories();
//
foreach (DirectoryInfo dir in dirs)
{
TreeNode child = new TreeNode();
child.Text = dir.Name;
child.Tag = dir.FullName;
parent.Nodes.Add(child);
//CreateTextNode(child);
CreateChild(child);
}
}
}