盘符所在区域是一个lable,命名为labelCurrentPath,最右边lable命名为ViewMode,5种显示方式,使用RadioButton控件,最下边的button作用为显示上一级,显示内容区域当然是ListView了,命名为listViewFilesAndFolders
构造函数:调用两个函数并用根文件夹实例化folderCol StringCollection字符串集合
代码语言:javascript复制private System.Collections.Specialized.StringCollection folderCol;
public Form1()
{
InitializeComponent();
folderCol = new System.Collections.Specialized.StringCollection();
CreateHeadersAndFillListView();
PatientListView(@"D:");
folderCol.Add(@"D:");
}
1.创建列标题
代码语言:javascript复制<pre name="code" class="csharp"> private void CreateHeadersAndFillListView()
{
ColumnHeader colHead;
colHead = new ColumnHeader();
colHead.Text = "Fileame";
listViewFilesAndFolders.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Size";
listViewFilesAndFolders.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Last accessed";
listViewFilesAndFolders.Columns.Add(colHead);
}
2.在第一次显示窗体时,最后初始化的是磁盘上的文件和文件夹填充列表视图,通过以下方法实现
代码语言:javascript复制//打开的文件路径
private void PatientListView(string root)
{
try
{
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
if (string.IsNullOrEmpty(root))
return;
DirectoryInfo dir = new DirectoryInfo(root);
DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
listViewFilesAndFolders.Items.Clear();
labelCurrentPath.Text = root;
listViewFilesAndFolders.BeginUpdate();
foreach (DirectoryInfo di in dirs)
{
lvi = new ListViewItem();
lvi.Text = di.Name;
lvi.ImageIndex = 0;
lvi.Tag = di.FullName;
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = "";
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = di.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
listViewFilesAndFolders.Items.Add(lvi);
}
foreach (FileInfo fi in files)
{
lvi = new ListViewItem();
lvi.Text = fi.Name;
lvi.ImageIndex = 1;
lvi.Tag = fi.FullName;
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.Length.ToString();
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
listViewFilesAndFolders.Items.Add(lvi);
}
listViewFilesAndFolders.EndUpdate();
}
catch (Exception err)
{
MessageBox.Show("Error: " err.Message);
//throw;
}
//根据列内容自动调整列宽
listViewFilesAndFolders.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
3.为了允许用户通过双击ListView中的选项来浏览文件夹,需订阅ItemActivate事件
代码语言:javascript复制 private void listViewFilesAndFolders_ItemActivate(object sender, EventArgs e)
{
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
string filename = lw.SelectedItems[0].Tag.ToString();
if (lw.SelectedItems[0].ImageIndex != 0)
{
try
{
System.Diagnostics.Process.Start(filename);
}
catch (Exception)
{
return;
}
}
else
{
PatientListView(filename);
folderCol.Add(filename);
}
}
4.Button处理;
代码语言:javascript复制//返回上一级
private void button1_Click(object sender, EventArgs e)
{
if (folderCol.Count > 1)
{
PatientListView(folderCol[folderCol.Count - 2].ToString());
folderCol.RemoveAt(folderCol.Count - 1);
}
else
{
PatientListView(folderCol[0].ToString());
}
}
5.最后修改列表视图的查看类型:
代码语言:javascript复制private void radioButtonLargeIcon_CheckedChanged_1(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.LargeIcon;
}
private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.SmallIcon;
}
private void radioButtonList_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.List;
}
private void radioButtonDetails_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Details;
}
private void radioButtonTile_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Tile;
}