Use ListView to display File info: name, size and date : ListView « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ListViewScreenshots 
Use ListView to display File info: name, size and date
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);
    }
  }
}

           
       
Related examples in the same category
1.Windows Explorer-Like Program: extends ListView
2.Dragging and dropping between ListView
3.ListView Item clicked eventListView Item clicked event
4.Folder Browser based on ListViewFolder Browser based on ListView
5.Set ColumnHeader for ListView
6.Sort a List View by Any ColumnSort a List View by Any Column
7.Add Data to a ListView
8.Extends ListViewItem
9.Use RadioButton to control the ListView display styleUse RadioButton to control the ListView display style
10.Display Directory info in a ListViewDisplay Directory info in a ListView
11.Add ListView column and insert ListView rowsAdd ListView column and insert ListView rows
12.Use ListViewItem to display file information
13.ListView ExampleListView Example
14.ListView Country: image and fontListView Country: image and font
15.Use ListView to display file name and double click the name to execute that fileUse ListView to display file name and double click the name to execute that file
16.Use ListView to diaplay folder info and double click to enter that directoryUse ListView to diaplay folder info and double click to enter that directory
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.