实现代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp20140821
{
public partial class FileSystem : Form
{
public FileSystem()
{
InitializeComponent();
this.txtDirectoryInfo.Text = "F:\\";
}
private void BtnGet_Click(object sender, EventArgs e)
{
ClearAll();
string folderName = this.txtDirectoryInfo.Text;
this.txtCurrentDirectoryInfo.Text = folderName;
try
{
DirectoryInfo folder = new DirectoryInfo(folderName);
if(folder.Exists)
{
GetDirectoryInfo(folderName);
}
FileInfo fileInfo = new FileInfo(folderName);
if(fileInfo.Exists)
{
GetFileInfo(folderName);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message");
}
}
private void GetFileInfo(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
if(fileInfo.Exists)
{
this.txtCreationTime.Text = fileInfo.CreationTime.ToLongDateString() + " "
+ fileInfo.CreationTime.ToLongTimeString();
this.txtFileSize.Text = fileInfo.Length.ToString() + " bytes";
this.txtLastAccessTime.Text = fileInfo.LastAccessTime.ToLongDateString();
this.txtLastWriteTime.Text = fileInfo.LastWriteTime.ToLongDateString();
}
}
private void GetDirectoryInfo(string folderName)
{
DirectoryInfo folder = new DirectoryInfo(folderName);
if(folder.Exists)
{
this.listBoxDirectoryInfos.Items.Clear();
foreach (DirectoryInfo info in folder.GetDirectories())
{
this.listBoxDirectoryInfos.Items.Add(info.Name);
}
this.listBoxFileInfos.Items.Clear();
foreach(FileInfo info in folder.GetFiles())
{
this.listBoxFileInfos.Items.Add(info.Name);
}
}
}
private void ClearAll()
{
this.txtCreationTime.Text = "";
this.txtFileSize.Text = "";
this.txtLastAccessTime.Text = "";
this.txtLastWriteTime.Text = "";
}
private void btnPrevious_Click(object sender, EventArgs e)
{
ClearAll();
string currentFolderName = this.txtCurrentDirectoryInfo.Text;
DirectoryInfo folder = new DirectoryInfo(currentFolderName);
if (folder.Parent == null)
{
return;
}
currentFolderName = folder.Parent.FullName;
this.txtCurrentDirectoryInfo.Text = currentFolderName;
if (folder.Exists)
{
GetDirectoryInfo(currentFolderName);
}
}
private void listBoxDirectoryInfos_MouseClick(object sender, MouseEventArgs e)
{
int index = listBoxDirectoryInfos.IndexFromPoint(e.X, e.Y);
if (index != -1)
{
ClearAll();
string fileName = this.listBoxDirectoryInfos.SelectedItem.ToString();
string folderName = Path.Combine(this.txtCurrentDirectoryInfo.Text, fileName);
this.txtCurrentDirectoryInfo.Text = folderName;
DirectoryInfo info = new DirectoryInfo(folderName);
if(info.Exists)
{
GetDirectoryInfo(folderName);
}
FileInfo fileInfo = new FileInfo(folderName);
if (fileInfo.Exists)
{
GetFileInfo(folderName);
}
}
}
private void listBoxFileInfos_MouseClick(object sender, MouseEventArgs e)
{
int index = listBoxFileInfos.IndexFromPoint(e.X, e.Y);
if (index != -1)
{
ClearAll();
string fileName = this.listBoxFileInfos.SelectedItem.ToString();
string folderName = Path.Combine(this.txtCurrentDirectoryInfo.Text, fileName);
FileInfo fileInfo = new FileInfo(folderName);
if (fileInfo.Exists)
{
GetFileInfo(folderName);
}
}
}
private void txtDirectoryInfo_MouseClick(object sender, MouseEventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowDialog();
this.txtDirectoryInfo.Text = dialog.SelectedPath;
BtnGet_Click(this, new EventArgs());
}
private void txtDirectoryInfo_TextChanged(object sender, EventArgs e)
{
this.txtCurrentDirectoryInfo.Text = this.txtDirectoryInfo.Text;
}
}
}
实现效果:
转载:http://blog.youkuaiyun.com/foreverling/article/details/38823687