您的位置:首页 > 编程语言 > C#

C#文件操作,父目录,子目录,所有文件、文件夹,属性信息

2011-03-25 19:27 579 查看
下面代码主要参考了C#高级编程第六版的源码部分改写增加了中文注释,

using System;
using System.IO;
using System.Windows.Forms;

namespace FileProperties
{
public partial class Form1 : Form
{
private string currentFolderPath;

public Form1()
{
InitializeComponent();
}

protected void ClearAllFields()
{
listBoxFolders.Items.Clear();
listBoxFiles.Items.Clear();
txtBoxFolder.Text = "";
txtBoxFileName.Text = "";
txtBoxCreationTime.Text = "";
txtBoxLastAccessTime.Text = "";
txtBoxLastWriteTime.Text = "";
txtBoxFileSize.Text = "";
}
/// <summary>
/// 显示文件信息
/// </summary>
/// <param name="fileFullName">文件完整路径+文件名</param>
protected void DisplayFileInfo(string fileFullName)
{
FileInfo theFile = new FileInfo(fileFullName);
if (!theFile.Exists)
throw new FileNotFoundException("文件: " + fileFullName + "没有找到!");
txtBoxFileName.Text = theFile.Name;
txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();
txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();
txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();
txtBoxFileSize.Text = theFile.Length + " bytes";
}
/// <summary>
/// 根据目录名显示其下所有文件、文件夹
/// </summary>
/// <param name="folderFullName"></param>
protected void DisplayFolderList(string folderFullName)
{
//先判断这个目录存在不
DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
if (!theFolder.Exists)
throw new DirectoryNotFoundException("Folder not found: "
+ folderFullName);
ClearAllFields();
txtBoxFolder.Text = theFolder.FullName;
currentFolderPath = theFolder.FullName;//当前路径设为全局,便于后面操作

//获取当前目录下的文件夹
foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
listBoxFolders.Items.Add(nextFolder.Name);

//获取当前目录下的文件
foreach (FileInfo nextFile in theFolder.GetFiles())
listBoxFiles.Items.Add(nextFile.Name);
}

protected void OnDisplayButtonClick(object sender , EventArgs e)
{
try
{
///判断是否是文件夹,是 直接返回。否则 继续
string folderPath = txtBoxInput.Text;
DirectoryInfo theFolder = new DirectoryInfo(folderPath);
if (theFolder.Exists)
{
DisplayFolderList(theFolder.FullName);
return;
}
//判断是否文件
FileInfo theFile = new FileInfo(folderPath);
if (theFile.Exists)
{
//FileInfo.Directory 获取当前文件所在的目录
DisplayFolderList(theFile.Directory.FullName);
//获取当前文件在文件系统中的索引
int index = listBoxFiles.Items.IndexOf(theFile.Name);
//设置其选择触发选择事件
listBoxFiles.SetSelected(index , true);
return;
}
throw new FileNotFoundException("没有文件或文件夹的名字是 "
+ ": " + txtBoxInput.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 文件被选择时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnListBoxFilesSelected(object sender , EventArgs e)
{
try
{
string selectedString = listBoxFiles.SelectedItem.ToString();
string fullFileName = Path.Combine(currentFolderPath , selectedString);
DisplayFileInfo(fullFileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//目录被选择时触发
protected void OnListBoxFoldersSelected(object sender , EventArgs e)
{
try
{
//获取选择的目录名
string selectedString = listBoxFolders.SelectedItem.ToString();
//
string fullPathName = Path.Combine(currentFolderPath , selectedString);
DisplayFolderList(fullPathName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

protected void OnUpButtonClick(object sender , EventArgs e)
{
try
{
//获取当前目录的父目录
string folderPath = new FileInfo(currentFolderPath).DirectoryName;
DisplayFolderList(folderPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

http://hi.baidu.com/jiang_yy_jiang/blog/item/f14c545137646b3143a75bd6.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐