当前位置:编程学习 > C#/ASP.NET >>

求助...利用ListView查看盘符下的文件夹和文件后,往上返回时无法显示,为什么?

定义:
private System.Collections.Specialized.StringCollection folderCol;

初始化:
folderCol =new System.Collections.Specialized.StringCollection();
CreateHeadersAndFillListView();
PaintListView(@"d:\");
folderCol.Add (@"d:\");

返回按钮代码为:
private void button1_Click(object sender, System.EventArgs e)
{
if(folderCol.Count>1)
{
PaintListView(folderCol[folderCol.Count-2].ToString());
folderCol.RemoveAt(folderCol.Count-1);
}
else
{
PaintListView(folderCol[0].ToString());
}
}

用到的函数:

private void CreateHeadersAndFillListView()
{
ColumnHeader colHead;
colHead =new ColumnHeader();
colHead.Text="Filename";
this.listView1.Columns.Add (colHead);
colHead=new ColumnHeader();
colHead.Text="Size";
this.listView1.Columns.Add (colHead);
colHead=new ColumnHeader ();
colHead.Text="Last accessed";
this.listView1.Columns .Add (colHead);
}

private void PaintListView(string root)
{
try
{
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
if(root.CompareTo("")==0)
return;

DirectoryInfo dir=new DirectoryInfo(root);
DirectoryInfo[] dirs=dir.GetDirectories();
FileInfo[] files=dir.GetFiles();
this.listView1.Items.Clear ();
this.label1.Text=root;
this.listView1.BeginUpdate();
foreach(System.IO.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);
this.listView1.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);
this.listView1.Items.Add(lvi);
this.listView1.EndUpdate();
}

}
catch(System.Exception err)
{
MessageBox.Show("Error:" +err.Message);
}

}

ListView代码:
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
ListView lw=(ListView)sender;
string filename=lw.SelectedItems[0].Tag.ToString();
if(lw.SelectedItems[0].ImageIndex!=0)
{
try
{
System.Diagnostics.Process.Start(filename);
}
catch
{
return;
}
}
else
{
PaintListView(filename);
folderCol.Add (filename);
}
} --------------------编程问答-------------------- 沒人回﹐自己頂下﹗ --------------------编程问答-------------------- 我也在看这一段,谁能解释一下button1_Click这段代码,我有些糊涂。
尤其是这段
PaintListView(folderCol[folderCol.Count-2].ToString()); 
folderCol.RemoveAt(folderCol.Count-1);

PaintListView(folderCol[0].ToString()); 
 
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,