ListView顯示本機文件夾及文件

Windows.Control 专栏收录该内容
2 篇文章 0 订阅

1. 從工具欄中拉兩個Imagelist控件,分別命名為ilLarge,ilSmall,分別添加兩個圖片以顯示文件的圖標, 屬性Size分別設置為(16,16),(32,32).   ListView屬性

LagerLmageList=ilLagrge,SmallImageList=ilsmall

2. 再拉一個ContextMenuStrip控件,以選擇ListView.View

  public   partial   class  ListView : Form
    
{
        System.Collections.Specialized.StringCollection foldCol;
        
public ListView()
        
{
            InitializeComponent();

            
foreach (string drive in Directory.GetLogicalDrives())
            
{
                Filelist.Items.Add(drive);
            }

            foldCol 
= new System.Collections.Specialized.StringCollection();
            createHeadersAndFillListView();
            PaintListView(
@""+Filelist.Items[0].ToString()+"");
            foldCol.Add(
@"" + Filelist.Items[0].ToString() + "");
            FileView.ItemActivate 
+= new EventHandler(FileView_ItemActivate);
            Filelist.DoubleClick 
+= new EventHandler(Filelist_DoubleClick);
        }


        
void Filelist_DoubleClick(object sender, EventArgs e)
        
{
            ListBox list 
= (ListBox)sender;
            
string Filename = (string)list.SelectedItem;
            PaintListView(
@"" + Filename + "");
            foldCol.Add(
@"" + Filename + "");
        }


        
void FileView_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
                
{
                    
return;
                }

            }

            
else
            
{
                PaintListView(filename);
                foldCol.Add(filename);
            }

        }


        
void createHeadersAndFillListView()
        
{
            ColumnHeader colHead;
            colHead 
= new ColumnHeader();
            colHead.Text 
= "Filename";
            FileView.Columns.Add(colHead);

            colHead 
= new ColumnHeader();
            colHead.Text 
= "Size";
            FileView.Columns.Add(colHead);

            colHead 
= new ColumnHeader();
            colHead.Text 
= "Last accessed";
            FileView.Columns.Add(colHead);
        }

        
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();

                FileView.Items.Clear();
                labPathName.Text 
= root;
                FileView.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);

                    FileView.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);

                    FileView.Items.Add(lvi);
                }

                FileView.EndUpdate();
            }

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


        
private void btnpicture_Click(object sender, EventArgs e)
        
{
            
if (foldCol.Count > 1)
            
{
                PaintListView(foldCol[foldCol.Count
-2].ToString());
                foldCol.RemoveAt(foldCol.Count 
- 1);
            }

            
else { PaintListView(foldCol[0].ToString()); }
        }


        
private void itemLargeIcon_Click(object sender, EventArgs e)
        
{
            FileView.View 
= View.LargeIcon;
        }


        
private void itemList_Click(object sender, EventArgs e)
        
{
            FileView.View 
= View.List;
        }


        
private void itemSmallIcon_Click(object sender, EventArgs e)
        
{
            FileView.View 
= View.SmallIcon;
        }


        
private void itemDetails_Click(object sender, EventArgs e)
        
{
            FileView.View 
= View.Details;
        }

    }

運行效果圖

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值