Use ListView to display File info: name, size and date


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.BeginUpdate();

        foreach (System.IO.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.lwFilesAndFolders.Items.Add(lvi);
        }        
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
            Console.WriteLine(filename);
    }
  }
}

           
          


ListView Country: image and font


/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ListView
{
///

/// Summary description for ListViewCountry.
///

public class ListViewCountry : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader Country;
private System.Windows.Forms.ColumnHeader Capital;
private System.Windows.Forms.ColumnHeader City_1;
private System.Windows.Forms.ColumnHeader City_2;
private System.Windows.Forms.Button REPORT;
private System.Windows.Forms.Button LIST;
private System.Windows.Forms.Button SMALLICON;
private System.Windows.Forms.Button LARGEICON;
ImageList ig = new ImageList();
private System.Windows.Forms.Button SELECTED;
private System.Windows.Forms.Button CHECKED;
private System.Windows.Forms.Panel panel1;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public ListViewCountry()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.Country = new System.Windows.Forms.ColumnHeader();
this.Capital = new System.Windows.Forms.ColumnHeader();
this.City_1 = new System.Windows.Forms.ColumnHeader();
this.City_2 = new System.Windows.Forms.ColumnHeader();
this.REPORT = new System.Windows.Forms.Button();
this.LIST = new System.Windows.Forms.Button();
this.SMALLICON = new System.Windows.Forms.Button();
this.LARGEICON = new System.Windows.Forms.Button();
this.SELECTED = new System.Windows.Forms.Button();
this.CHECKED = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.AllowColumnReorder = true;
this.listView1.CheckBoxes = true;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.Country,
this.Capital,
this.City_1,
this.City_2});
this.listView1.Dock = System.Windows.Forms.DockStyle.Top;
this.listView1.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.listView1.FullRowSelect = true;
this.listView1.Name = “listView1”;
this.listView1.Size = new System.Drawing.Size(464, 152);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// Country
//
this.Country.Text = “Country”;
this.Country.Width = 100;
//
// Capital
//
this.Capital.Text = “Capital”;
this.Capital.Width = 150;
//
// City_1
//
this.City_1.Text = “City_1”;
this.City_1.Width = 100;
//
// City_2
//
this.City_2.Text = “City_2”;
this.City_2.Width = 100;
//
// REPORT
//
this.REPORT.Dock = System.Windows.Forms.DockStyle.Left;
this.REPORT.Location = new System.Drawing.Point(75, 0);
this.REPORT.Name = “REPORT”;
this.REPORT.Size = new System.Drawing.Size(75, 24);
this.REPORT.TabIndex = 1;
this.REPORT.Text = “Report”;
this.REPORT.Click += new System.EventHandler(this.REPORT_Click);
//
// LIST
//
this.LIST.Dock = System.Windows.Forms.DockStyle.Left;
this.LIST.Location = new System.Drawing.Point(300, 0);
this.LIST.Name = “LIST”;
this.LIST.Size = new System.Drawing.Size(75, 24);
this.LIST.TabIndex = 1;
this.LIST.Text = “List”;
this.LIST.Click += new System.EventHandler(this.LIST_Click);
//
// SMALLICON
//
this.SMALLICON.Dock = System.Windows.Forms.DockStyle.Left;
this.SMALLICON.Location = new System.Drawing.Point(150, 0);
this.SMALLICON.Name = “SMALLICON”;
this.SMALLICON.Size = new System.Drawing.Size(75, 24);
this.SMALLICON.TabIndex = 1;
this.SMALLICON.Text = “Small Icon”;
this.SMALLICON.Click += new System.EventHandler(this.SMALLICON_Click);
//
// LARGEICON
//
this.LARGEICON.Dock = System.Windows.Forms.DockStyle.Left;
this.LARGEICON.Location = new System.Drawing.Point(375, 0);
this.LARGEICON.Name = “LARGEICON”;
this.LARGEICON.Size = new System.Drawing.Size(75, 24);
this.LARGEICON.TabIndex = 1;
this.LARGEICON.Text = “Large Icon”;
this.LARGEICON.Click += new System.EventHandler(this.LARGEICON_Click);
//
// SELECTED
//
this.SELECTED.Dock = System.Windows.Forms.DockStyle.Left;
this.SELECTED.Name = “SELECTED”;
this.SELECTED.Size = new System.Drawing.Size(75, 24);
this.SELECTED.TabIndex = 2;
this.SELECTED.Text = “Selected”;
this.SELECTED.Click += new System.EventHandler(this.SELECTED_Click);
//
// CHECKED
//
this.CHECKED.Dock = System.Windows.Forms.DockStyle.Left;
this.CHECKED.Location = new System.Drawing.Point(225, 0);
this.CHECKED.Name = “CHECKED”;
this.CHECKED.Size = new System.Drawing.Size(75, 24);
this.CHECKED.TabIndex = 3;
this.CHECKED.Text = “Checked”;
this.CHECKED.Click += new System.EventHandler(this.CHECKED_Click);
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.LARGEICON,
this.LIST,
this.CHECKED,
this.SMALLICON,
this.REPORT,
this.SELECTED});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 157);
this.panel1.Name = “panel1”;
this.panel1.Size = new System.Drawing.Size(464, 24);
this.panel1.TabIndex = 4;
//
// ListViewCountry
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 181);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1,
this.listView1});
this.Name = “ListViewCountry”;
this.Text = “ListView”;
this.Load += new System.EventHandler(this.ListViewCountry_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new ListViewCountry());
}

private void ListViewCountry_Load(object sender, System.EventArgs e)
{
// Create an image list of icons that you want to display for each item
ig.Images.Add(new Icon(“FLGUSA01.ICO”));
ig.Images.Add(new Icon(“CTRITALY.ICO”));
ig.Images.Add(new Icon(“FLGCAN.ICO”));
ig.Images.Add(new Icon(“FLGSWITZ.ICO”));
ig.Images.Add(new Icon(“FLGUK.ICO”));

// Set the Imagelist for SmallIcons
listView1.SmallImageList = ig ;
// Set the ImageList for LargeIcons
listView1.LargeImageList = ig ;

// Align the columns 1,2,3 and the column data as Center
listView1.Columns[1].TextAlign = HorizontalAlignment.Center ;
listView1.Columns[2].TextAlign = HorizontalAlignment.Center ;
listView1.Columns[3].TextAlign = HorizontalAlignment.Center ;

// Create a ListViewItem object for evey item that you wish to add the ListView.
string[] lv = new String[4];
lv[0] = “USA” ;
lv[1] = “Washington DC” ;
lv[2] = “New York” ;
lv[3] = “Los Angeles” ;
listView1.Items.Add( new ListViewItem(lv,0));

lv[0] = “Italy” ;
lv[1] = “Rome” ;
lv[2] = “Venice” ;
lv[3] = “Milan” ;
listView1.Items.Add( new ListViewItem(lv,1));

lv[0] = “Canada” ;
lv[1] = “Ottawa” ;
lv[2] = “Montreal” ;
lv[3] = “Quebec” ;
listView1.Items.Add( new ListViewItem(lv,2));

lv[0] = “Switzerland” ;
lv[1] = “Geneva” ;
lv[2] = “Zurich” ;
lv[3] = “Lucerne” ;
listView1.Items.Add( new ListViewItem(lv,3));

lv[0] = “UnitedKingdom” ;
lv[1] = “London” ;
lv[2] = “ChesterField” ;
lv[3] = “Wembly” ;
listView1.Items.Add( new ListViewItem(lv,4));

for ( int j=0; j < listView1.Items.Count ; j++) { ListViewItem lvi = listView1.Items[j] ; for ( int i=0; i < lvi.SubItems.Count ; i++) { Console.Write(lvi.SubItems[i].Text + " ") ; } Console.WriteLine(" "); } } private void REPORT_Click(object sender, System.EventArgs e) { // Switch to the Report View this.Text = "Report View"; listView1.View = View.Details; } private void LARGEICON_Click(object sender, System.EventArgs e) { // Switch to the LargeIcon view this.Text = "LargeIcon View"; listView1.View = View.LargeIcon ; } private void SMALLICON_Click(object sender, System.EventArgs e) { // Switch to the SmallIcon view this.Text = "SmallIcon View"; listView1.View = View.SmallIcon; } private void LIST_Click(object sender, System.EventArgs e) { // Switch to the List view this.Text = "List View"; listView1.View = View.List; } private void SELECTED_Click(object sender, System.EventArgs e) { // Get the list of indices for all the selected items string StrSelected = "ITEM SELECTED FROM THE LISTVIEW ARE ... " ; for ( int k=0; k < listView1.SelectedIndices.Count ; k++) { StrSelected = StrSelected + "[" + (k+1) + "] " ; // Get the item ( as ListViewItem ) at the selected index. ListViewItem lvi = listView1.Items[listView1.SelectedIndices[k]]; for ( int i=0; i < lvi.SubItems.Count ; i++) { // Get the subItem for the the selected item StrSelected = StrSelected + lvi.SubItems[i].Text + " "; } StrSelected = StrSelected + " "; } MessageBox.Show(StrSelected , "Selected Items"); } private void CHECKED_Click(object sender, System.EventArgs e) { // Get the list of indices for all checked items string StrSelected = "ITEM CHECKED FROM THE LISTVIEW ARE ... " ; for ( int k=0; k < listView1.CheckedIndices.Count; k++) { StrSelected = StrSelected + "[" + (k+1) + "] " ; // Get the item ( as ListViewItem ) at the selected index. ListViewItem lvi = listView1.Items[listView1.CheckedIndices[k]]; for ( int i=0; i < lvi.SubItems.Count ; i++) { // Get the subItem for the the selected item StrSelected = StrSelected + lvi.SubItems[i].Text + " "; } StrSelected = StrSelected + " "; } MessageBox.Show(StrSelected , "Checked Items"); } private void listView1_SelectedIndexChanged(object sender, System.EventArgs e) { } private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e) { if (listView1.Sorting == SortOrder.Descending) listView1.Sorting = SortOrder.Ascending; else listView1.Sorting = SortOrder.Descending; listView1.Sort(); } } } ListView.zip( 46 k)[/csharp]

ListView Example



   


/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ListViewExample
{
  /// <summary>
  /// Summary description for ListViewExample.
  /// </summary>
  public class ListViewExample : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.GroupBox GroupBox1;
    internal System.Windows.Forms.RadioButton optLargeIcon;
    internal System.Windows.Forms.RadioButton optList;
    internal System.Windows.Forms.RadioButton optDetails;
    internal System.Windows.Forms.RadioButton optSmallIcon;
    internal System.Windows.Forms.Button cmdFillList;
    internal System.Windows.Forms.ListView listAuthors;
    internal System.Windows.Forms.ImageList imagesLarge;
    internal System.Windows.Forms.ImageList imagesSmall;
    private System.ComponentModel.IContainer components;

    public ListViewExample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ListViewExample));
      this.GroupBox1 = new System.Windows.Forms.GroupBox();
      this.optLargeIcon = new System.Windows.Forms.RadioButton();
      this.optList = new System.Windows.Forms.RadioButton();
      this.optDetails = new System.Windows.Forms.RadioButton();
      this.optSmallIcon = new System.Windows.Forms.RadioButton();
      this.cmdFillList = new System.Windows.Forms.Button();
      this.listAuthors = new System.Windows.Forms.ListView();
      this.imagesLarge = new System.Windows.Forms.ImageList(this.components);
      this.imagesSmall = new System.Windows.Forms.ImageList(this.components);
      this.GroupBox1.SuspendLayout();
      this.SuspendLayout();
      // 
      // GroupBox1
      // 
      this.GroupBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                          this.optLargeIcon,
                                          this.optList,
                                          this.optDetails,
                                          this.optSmallIcon});
      this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.GroupBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.GroupBox1.Location = new System.Drawing.Point(276, 12);
      this.GroupBox1.Name = "GroupBox1";
      this.GroupBox1.Size = new System.Drawing.Size(104, 132);
      this.GroupBox1.TabIndex = 5;
      this.GroupBox1.TabStop = false;
      this.GroupBox1.Text = "View";
      // 
      // optLargeIcon
      // 
      this.optLargeIcon.Checked = true;
      this.optLargeIcon.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.optLargeIcon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.optLargeIcon.Location = new System.Drawing.Point(16, 48);
      this.optLargeIcon.Name = "optLargeIcon";
      this.optLargeIcon.Size = new System.Drawing.Size(76, 16);
      this.optLargeIcon.TabIndex = 0;
      this.optLargeIcon.TabStop = true;
      this.optLargeIcon.Text = "LargeIcon";
      this.optLargeIcon.CheckedChanged += new System.EventHandler(this.NewView);
      // 
      // optList
      // 
      this.optList.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.optList.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.optList.Location = new System.Drawing.Point(16, 96);
      this.optList.Name = "optList";
      this.optList.Size = new System.Drawing.Size(56, 16);
      this.optList.TabIndex = 0;
      this.optList.Text = "List";
      this.optList.CheckedChanged += new System.EventHandler(this.NewView);
      // 
      // optDetails
      // 
      this.optDetails.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.optDetails.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.optDetails.Location = new System.Drawing.Point(16, 72);
      this.optDetails.Name = "optDetails";
      this.optDetails.Size = new System.Drawing.Size(72, 16);
      this.optDetails.TabIndex = 0;
      this.optDetails.Text = "Details";
      this.optDetails.CheckedChanged += new System.EventHandler(this.NewView);
      // 
      // optSmallIcon
      // 
      this.optSmallIcon.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.optSmallIcon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.optSmallIcon.Location = new System.Drawing.Point(16, 24);
      this.optSmallIcon.Name = "optSmallIcon";
      this.optSmallIcon.Size = new System.Drawing.Size(72, 16);
      this.optSmallIcon.TabIndex = 0;
      this.optSmallIcon.Text = "SmallIcon";
      this.optSmallIcon.CheckedChanged += new System.EventHandler(this.NewView);
      // 
      // cmdFillList
      // 
      this.cmdFillList.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.cmdFillList.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.cmdFillList.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.cmdFillList.Location = new System.Drawing.Point(276, 152);
      this.cmdFillList.Name = "cmdFillList";
      this.cmdFillList.Size = new System.Drawing.Size(104, 24);
      this.cmdFillList.TabIndex = 4;
      this.cmdFillList.Text = "Fill List";
      this.cmdFillList.Click += new System.EventHandler(this.cmdFillList_Click);
      // 
      // listAuthors
      // 
      this.listAuthors.Activation = System.Windows.Forms.ItemActivation.OneClick;
      this.listAuthors.AllowColumnReorder = true;
      this.listAuthors.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.listAuthors.GridLines = true;
      this.listAuthors.HoverSelection = true;
      this.listAuthors.Location = new System.Drawing.Point(8, 8);
      this.listAuthors.Name = "listAuthors";
      this.listAuthors.Size = new System.Drawing.Size(260, 332);
      this.listAuthors.Sorting = System.Windows.Forms.SortOrder.Ascending;
      this.listAuthors.TabIndex = 3;
      this.listAuthors.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listAuthors_ColumnClick);
      // 
      // imagesLarge
      // 
      this.imagesLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imagesLarge.ImageSize = new System.Drawing.Size(32, 32);
      this.imagesLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesLarge.ImageStream")));
      this.imagesLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // imagesSmall
      // 
      this.imagesSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imagesSmall.ImageSize = new System.Drawing.Size(16, 16);
      this.imagesSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesSmall.ImageStream")));
      this.imagesSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ListViewExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(388, 349);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.GroupBox1,
                                      this.cmdFillList,
                                      this.listAuthors});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Name = "ListViewExample";
      this.Text = "ListView Example";
      this.Load += new System.EventHandler(this.ListViewExample_Load);
      this.GroupBox1.ResumeLayout(false);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new ListViewExample());
    }

    private void cmdFillList_Click(object sender, System.EventArgs e)
    {
      DataTable dt = StoreDB.GetProducts();

      // Suspending automatic refreshes as items are added/removed.
      listAuthors.BeginUpdate();

      listAuthors.SmallImageList = imagesSmall;
      listAuthors.LargeImageList = imagesLarge;
      foreach (DataRow dr in dt.Rows)
      {
        ListViewItem listItem = new ListViewItem(dr["ModelName"].ToString());
        listItem.ImageIndex = 0;

        // Add sub-items for Details view.
        listItem.SubItems.Add(dr["ProductID"].ToString());
        listItem.SubItems.Add(dr["Description"].ToString());

        listAuthors.Items.Add(listItem);
      }

      // Add column headers for Details view.
      listAuthors.Columns.Add("Product", 100, HorizontalAlignment.Left);
      listAuthors.Columns.Add("ID", 100, HorizontalAlignment.Left);
      listAuthors.Columns.Add("Description", 100, HorizontalAlignment.Left);

      // Re-enable the display.
      listAuthors.EndUpdate();

    }

    private void ListViewExample_Load(object sender, System.EventArgs e)
    {
      optLargeIcon.Tag = View.LargeIcon;
      optSmallIcon.Tag = View.SmallIcon;
      optDetails.Tag = View.Details;
      optList.Tag = View.List;
       cmdFillList_Click(null, null);
    }

    private void NewView(object sender, System.EventArgs e)
    {
      // Set the current view mode based on the number in the tag value of the
      // selected radio button.

      listAuthors.View = (View)(((Control)sender).Tag);

      // Display the current view style.
      this.Text = "Using View: " + listAuthors.View.ToString();
    }

    private void listAuthors_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
    {
      // Specify an alphabetic sort based on the column that was clicked.
      listAuthors.ListViewItemSorter = new CompareListViewItems(e.Column, true);

      // Perform the sort.
      listAuthors.Sort();
    }

  }

  public class CompareListViewItems : IComparer
  {
    // This index identifies the column that is used for sorting
    public readonly int Column;

    // Is the sort alphabetic or number?
    public readonly bool Alphabetic;

    public CompareListViewItems(int columnIndex, bool alphabetic)
    {
      this.Column = columnIndex;
      this.Alphabetic = alphabetic;
    }

    public int Compare(object x, object y)
    {
      // Convert the items that must be compared into ListViewItem objects.
      string listX = ((ListViewItem)x).SubItems[Column].Text;
      string listY = ((ListViewItem)y).SubItems[Column].Text;

      // Sort using the specified column and specified sorting type.
      if (Alphabetic)
      {
        return listX.CompareTo(listY);
        
      }
      else
      {
        if (int.Parse(listX) > int.Parse(listY))
        {
          return 1;
        }
        else if (int.Parse(listX) == int.Parse(listY))
        {
          return 0;
        }
        else
        {
          return -1;
        }
      }
    }
  }


  public class StoreDB
  {
    public static DataTable GetProducts()
    {
      DataSet dsStore = new DataSet();
      dsStore.ReadXmlSchema(Application.StartupPath + "store.xsd");
      dsStore.ReadXml(Application.StartupPath + "store.xml");
      return dsStore.Tables["Products"];
    }
  }
}


           
          


ListViewExample.zip( 47 k)

Use ListViewItem to display file information

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;


class Form1 : Form {
    private System.Collections.Specialized.StringCollection folderCol;

    public Form1() {
        InitializeComponent();

        folderCol = new System.Collections.Specialized.StringCollection();
        CreateHeadersAndFillListView();
        PaintListView(@"C:");
        folderCol.Add(@"C:");
    }

    private void CreateHeadersAndFillListView() {
        ColumnHeader colHead;

        colHead = new ColumnHeader();
        colHead.Text = "Filename";
        this.listViewFilesAndFolders.Columns.Add(colHead); 

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

        colHead = new ColumnHeader();
        colHead.Text = "Last accessed";
        this.listViewFilesAndFolders.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.listViewFilesAndFolders.Items.Clear();

            this.labelCurrentPath.Text = root;

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

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

                this.listViewFilesAndFolders.Items.Add(lvi);
            }
            this.listViewFilesAndFolders.EndUpdate();
        } catch (System.Exception err) {
            MessageBox.Show("Error: " + err.Message);
        }
    }

    private void listViewFilesAndFoldes_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);
            folderCol.Add(filename);
        }
    }

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

    private void radioButtonLargeIcon_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.LargeIcon;
    }

    private void radioButtonList_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.List;
    }

    private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.SmallIcon;
    }

    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;
    }
    private void InitializeComponent() {
        System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("listViewFilesAndFoldes");
        this.labelCurrentPath = new System.Windows.Forms.Label();
        this.listViewFilesAndFolders = new System.Windows.Forms.ListView();
        this.buttonBack = new System.Windows.Forms.Button();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.radioButtonSmallIcon = new System.Windows.Forms.RadioButton();
        this.radioButtonList = new System.Windows.Forms.RadioButton();
        this.radioButtonTile = new System.Windows.Forms.RadioButton();
        this.radioButtonDetails = new System.Windows.Forms.RadioButton();
        this.radioButtonLargeIcon = new System.Windows.Forms.RadioButton();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();

        this.labelCurrentPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.labelCurrentPath.Location = new System.Drawing.Point(13, 13);
        this.labelCurrentPath.Name = "labelCurrentPath";
        this.labelCurrentPath.Size = new System.Drawing.Size(502, 15);
        this.labelCurrentPath.TabIndex = 0;

        this.listViewFilesAndFolders.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.listViewFilesAndFolders.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
        listViewItem2
      });
        this.listViewFilesAndFolders.Location = new System.Drawing.Point(13, 35);
        this.listViewFilesAndFolders.Name = "listViewFilesAndFoldes";
        this.listViewFilesAndFolders.Size = new System.Drawing.Size(423, 220);
        this.listViewFilesAndFolders.TabIndex = 1;
        this.listViewFilesAndFolders.View = System.Windows.Forms.View.Details;
        this.listViewFilesAndFolders.ItemActivate += new System.EventHandler(this.listViewFilesAndFoldes_ItemActivate);

        this.buttonBack.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
        this.buttonBack.Location = new System.Drawing.Point(222, 262);
        this.buttonBack.Name = "buttonBack";
        this.buttonBack.TabIndex = 2;
        this.buttonBack.Text = "Back";
        this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);

        this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.groupBox1.Controls.Add(this.radioButtonSmallIcon);
        this.groupBox1.Controls.Add(this.radioButtonList);
        this.groupBox1.Controls.Add(this.radioButtonTile);
        this.groupBox1.Controls.Add(this.radioButtonDetails);
        this.groupBox1.Controls.Add(this.radioButtonLargeIcon);
        this.groupBox1.Location = new System.Drawing.Point(443, 35);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(97, 220);
        this.groupBox1.TabIndex = 3;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "View Mode";

        this.radioButtonSmallIcon.AutoSize = true;
        this.radioButtonSmallIcon.Location = new System.Drawing.Point(7, 44);
        this.radioButtonSmallIcon.Name = "radioButtonSmallIcon";
        this.radioButtonSmallIcon.Size = new System.Drawing.Size(67, 17);
        this.radioButtonSmallIcon.TabIndex = 4;
        this.radioButtonSmallIcon.Text = "SmallIcon";
        this.radioButtonSmallIcon.CheckedChanged += new System.EventHandler(this.radioButtonSmallIcon_CheckedChanged);

        this.radioButtonList.AutoSize = true;
        this.radioButtonList.Location = new System.Drawing.Point(7, 68);
        this.radioButtonList.Name = "radioButtonList";
        this.radioButtonList.Size = new System.Drawing.Size(37, 17);
        this.radioButtonList.TabIndex = 3;
        this.radioButtonList.Text = "List";
        this.radioButtonList.CheckedChanged += new System.EventHandler(this.radioButtonList_CheckedChanged);

        this.radioButtonTile.AutoSize = true;
        this.radioButtonTile.Location = new System.Drawing.Point(7, 116);
        this.radioButtonTile.Name = "radioButtonTile";
        this.radioButtonTile.Size = new System.Drawing.Size(38, 17);
        this.radioButtonTile.TabIndex = 2;
        this.radioButtonTile.Text = "Tile";
        this.radioButtonTile.CheckedChanged += new System.EventHandler(this.radioButtonTile_CheckedChanged);

        this.radioButtonDetails.AutoSize = true;
        this.radioButtonDetails.Checked = true;
        this.radioButtonDetails.Location = new System.Drawing.Point(7, 92);
        this.radioButtonDetails.Name = "radioButtonDetails";
        this.radioButtonDetails.Size = new System.Drawing.Size(53, 17);
        this.radioButtonDetails.TabIndex = 1;
        this.radioButtonDetails.TabStop = true;
        this.radioButtonDetails.Text = "Details";
        this.radioButtonDetails.CheckedChanged += new System.EventHandler(this.radioButtonDetails_CheckedChanged);

        this.radioButtonLargeIcon.AutoSize = true;
        this.radioButtonLargeIcon.Location = new System.Drawing.Point(7, 20);
        this.radioButtonLargeIcon.Name = "radioButtonLargeIcon";
        this.radioButtonLargeIcon.Size = new System.Drawing.Size(69, 17);
        this.radioButtonLargeIcon.TabIndex = 0;
        this.radioButtonLargeIcon.Text = "LargeIcon";
        this.radioButtonLargeIcon.CheckedChanged += new System.EventHandler(this.radioButtonLargeIcon_CheckedChanged);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(550, 291);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.buttonBack);
        this.Controls.Add(this.listViewFilesAndFolders);
        this.Controls.Add(this.labelCurrentPath);
        this.Name = "Form1";
        this.Text = "ListView";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    private System.Windows.Forms.Label labelCurrentPath;
    private System.Windows.Forms.ListView listViewFilesAndFolders;
    private System.Windows.Forms.Button buttonBack;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.RadioButton radioButtonLargeIcon;
    private System.Windows.Forms.RadioButton radioButtonDetails;
    private System.Windows.Forms.RadioButton radioButtonTile;
    private System.Windows.Forms.RadioButton radioButtonList;
    private System.Windows.Forms.RadioButton radioButtonSmallIcon;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}

    


Add ListView column and insert ListView rows


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.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 = "sub item"; 
          lvi.SubItems.Add(lvsi); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = di.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

          this.lwFilesAndFolders.Items.Add(lvi);
        }
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
            Console.WriteLine(filename);
    }
  }
}


           
          


Display Directory info in a ListView


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.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 = "sub item"; 
          lvi.SubItems.Add(lvsi); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = di.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

          this.lwFilesAndFolders.Items.Add(lvi);
        }
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
            Console.WriteLine(filename);
    }
  }
}

           
          


Use RadioButton to control the ListView display style


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.RadioButton rdoLarge;
    private System.Windows.Forms.RadioButton rdoSmall;
    private System.Windows.Forms.RadioButton rdoList;
    private System.Windows.Forms.RadioButton rdoDetails;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();
      CreateHeadersAndFillListView();
      PaintListView();
    }

    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.groupBox1 = new System.Windows.Forms.GroupBox();
      this.rdoDetails = new System.Windows.Forms.RadioButton();
      this.rdoList = new System.Windows.Forms.RadioButton();
      this.rdoSmall = new System.Windows.Forms.RadioButton();
      this.rdoLarge = new System.Windows.Forms.RadioButton();
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.groupBox1.SuspendLayout();
      this.SuspendLayout();

      this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {this.rdoDetails,
                                                                            this.rdoList,
                                                                            this.rdoSmall,
                                                                            this.rdoLarge});
      this.groupBox1.Location = new System.Drawing.Point(424, 32);
      this.groupBox1.Name = "groupBox1";
      this.groupBox1.Size = new System.Drawing.Size(120, 128);
      this.groupBox1.TabIndex = 2;
      this.groupBox1.TabStop = false;
      this.groupBox1.Text = "View mode";
      // 
      // rdoDetails
      // 
      this.rdoDetails.Checked = true;
      this.rdoDetails.Location = new System.Drawing.Point(8, 96);
      this.rdoDetails.Name = "rdoDetails";
      this.rdoDetails.Size = new System.Drawing.Size(104, 16);
      this.rdoDetails.TabIndex = 3;
      this.rdoDetails.TabStop = true;
      this.rdoDetails.Text = "Details";
      this.rdoDetails.CheckedChanged += new System.EventHandler(this.rdoDetails_CheckedChanged);
      // 
      // rdoList
      // 
      this.rdoList.Location = new System.Drawing.Point(8, 72);
      this.rdoList.Name = "rdoList";
      this.rdoList.Size = new System.Drawing.Size(104, 16);
      this.rdoList.TabIndex = 2;
      this.rdoList.Text = "List";
      this.rdoList.CheckedChanged += new System.EventHandler(this.rdoList_CheckedChanged);
      // 
      // rdoSmall
      // 
      this.rdoSmall.Location = new System.Drawing.Point(8, 48);
      this.rdoSmall.Name = "rdoSmall";
      this.rdoSmall.Size = new System.Drawing.Size(104, 16);
      this.rdoSmall.TabIndex = 1;
      this.rdoSmall.Text = "SmallIcon";
      this.rdoSmall.CheckedChanged += new System.EventHandler(this.rdoSmall_CheckedChanged);
      // 
      // rdoLarge
      // 
      this.rdoLarge.Location = new System.Drawing.Point(8, 24);
      this.rdoLarge.Name = "rdoLarge";
      this.rdoLarge.Size = new System.Drawing.Size(96, 16);
      this.rdoLarge.TabIndex = 0;
      this.rdoLarge.Text = "LargeIcon";
      this.rdoLarge.CheckedChanged += new System.EventHandler(this.rdoLarge_CheckedChanged);
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.groupBox1, this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.groupBox1.ResumeLayout(false);
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView()
    {
      ListViewItem lvi;
      ListViewItem.ListViewSubItem lvsi;

      this.lwFilesAndFolders.Items.Clear();
      this.lwFilesAndFolders.BeginUpdate();

      lvi = new ListViewItem();
      lvi.Text = "A"; 
      lvi.ImageIndex = 0; 
      lvi.Tag = "tag";

      lvsi = new ListViewItem.ListViewSubItem();
      lvsi.Text = "4"; 
      lvi.SubItems.Add(lvsi); 
          
      lvsi = new ListViewItem.ListViewSubItem();
      lvsi.Text = "C"; 
      lvi.SubItems.Add(lvsi); 
      this.lwFilesAndFolders.Items.Add(lvi);

      this.lwFilesAndFolders.EndUpdate();
      this.lwFilesAndFolders.View = View.Details;
    }

    private void rdoLarge_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.LargeIcon;
    }

    private void rdoList_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.List;
    }

    private void rdoSmall_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.SmallIcon;
    }

    private void rdoDetails_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.Details;
    }
  }
}