http://blog.csdn.net/denuvead/article/details/2302288
1.一個button 命名爲 btok
2.一個textbox 命名爲 tbInput
3.一個Listbox 命名爲 lbResult
ok 我們分別爲button和listbox建立事件來實現顯示textbox中路徑下文件和listbox中選中文件的部分屬性
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace complete
{
public partial class Form1 : Form
{
string currentpath;
public Form1()
{
InitializeComponent();
}
private void btOK_Click(object sender, EventArgs e)
{
//從TextBox中提取輸入的字符
string path = tbInput.Text;
//檢查路徑名是否合法
if (path.Length > 0)
{
if (Directory.Exists(path))
{
lbResult.Items.Clear();
//獲取目錄中的所有文件名列表
string[] files = Directory.GetFiles(path);
//將文件去掉路徑名放在列表框中
foreach (string file in files)
{
FileAttributes attr = File.GetAttributes(file);
if ((attr & FileAttributes.Hidden) == 0)
lbResult.Items.Add(Path.GetFileName(file));
}
//在讀者雙擊一個文件名的情況下保存路徑名
currentpath = Path.GetFullPath(tbInput.Text);
}
//如果路徑無效,則告知讀者
else
MessageBox.Show(path+" is not a valid path","錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void lbResult_DoubleClick(object sender, EventArgs e)
{
//根據讀者雙擊的文件名創建一個完全限定的文件名
string file = currentpath;
if (!file.EndsWith(":") && !file.EndsWith("//"))
file += "/";
file += lbResult.SelectedItem.ToString();
//顯示文件創建時間和最後一次修改的時間
DateTime created = File.GetCreationTime(file);
DateTime modified = File.GetLastWriteTime(file);
string msg = "創建時間:" + created.ToLongDateString() + " 於:" + created.ToLongTimeString() + " " + "修改:" + modified.ToLongDateString() + " 於:" + modified.ToLongTimeString();
MessageBox.Show(msg, lbResult.SelectedItem.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}