ListView for File and Folder : ListView « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » ListView 
23.29.4.ListView for File and Folder
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;



   public 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.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.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 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
            {
               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 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;
      }
      private void InitializeComponent()
      {
         this.labelCurrentPath = new System.Windows.Forms.Label();
         this.listViewFilesAndFolders = new System.Windows.Forms.ListView();
         this.groupBoxViewMode = new System.Windows.Forms.GroupBox();
         this.radioButtonTile = new System.Windows.Forms.RadioButton();
         this.radioButtonDetails = new System.Windows.Forms.RadioButton();
         this.radioButtonList = new System.Windows.Forms.RadioButton();
         this.radioButtonSmallIcon = new System.Windows.Forms.RadioButton();
         this.radioButtonLargeIcon = new System.Windows.Forms.RadioButton();
         this.buttonBack = new System.Windows.Forms.Button();
         this.groupBoxViewMode.SuspendLayout();
         this.SuspendLayout();
         // 
         this.labelCurrentPath.Location = new System.Drawing.Point(12, 9);
         this.labelCurrentPath.Name = "labelCurrentPath";
         this.labelCurrentPath.Size = new System.Drawing.Size(429, 13);
         this.labelCurrentPath.TabIndex = 0;
         // 
         this.listViewFilesAndFolders.LargeImageList = this.imageListLarge;
         this.listViewFilesAndFolders.Location = new System.Drawing.Point(12, 25);
         this.listViewFilesAndFolders.Size = new System.Drawing.Size(429, 208);
         this.listViewFilesAndFolders.UseCompatibleStateImageBehavior = false;
         this.listViewFilesAndFolders.View = System.Windows.Forms.View.Details;
         this.listViewFilesAndFolders.ItemActivate += new System.EventHandler(this.listViewFilesAndFolders_ItemActivate);
         // 
         this.groupBoxViewMode.Controls.Add(this.radioButtonTile);
         this.groupBoxViewMode.Controls.Add(this.radioButtonDetails);
         this.groupBoxViewMode.Controls.Add(this.radioButtonList);
         this.groupBoxViewMode.Controls.Add(this.radioButtonSmallIcon);
         this.groupBoxViewMode.Controls.Add(this.radioButtonLargeIcon);
         this.groupBoxViewMode.Location = new System.Drawing.Point(447, 25);
         this.groupBoxViewMode.Name = "groupBoxViewMode";
         this.groupBoxViewMode.Size = new System.Drawing.Size(105, 208);
         this.groupBoxViewMode.TabIndex = 2;
         this.groupBoxViewMode.TabStop = false;
         this.groupBoxViewMode.Text = "View Mode";
         // 
         // radioButtonTile
         // 
         this.radioButtonTile.AutoSize = true;
         this.radioButtonTile.Location = new System.Drawing.Point(6, 111);
         this.radioButtonTile.Name = "radioButtonTile";
         this.radioButtonTile.Size = new System.Drawing.Size(42, 17);
         this.radioButtonTile.TabIndex = 4;
         this.radioButtonTile.Text = "Tile";
         this.radioButtonTile.UseVisualStyleBackColor = true;
         this.radioButtonTile.CheckedChanged += new System.EventHandler(this.radioButtonTile_CheckedChanged);
         // 
         // radioButtonDetails
         // 
         this.radioButtonDetails.AutoSize = true;
         this.radioButtonDetails.Checked = true;
         this.radioButtonDetails.Location = new System.Drawing.Point(6, 88);
         this.radioButtonDetails.Name = "radioButtonDetails";
         this.radioButtonDetails.Size = new System.Drawing.Size(57, 17);
         this.radioButtonDetails.TabIndex = 3;
         this.radioButtonDetails.TabStop = true;
         this.radioButtonDetails.Text = "Details";
         this.radioButtonDetails.UseVisualStyleBackColor = true;
         this.radioButtonDetails.CheckedChanged += new System.EventHandler(this.radioButtonDetails_CheckedChanged);
         // 
         // radioButtonList
         // 
         this.radioButtonList.AutoSize = true;
         this.radioButtonList.Location = new System.Drawing.Point(6, 65);
         this.radioButtonList.Name = "radioButtonList";
         this.radioButtonList.Size = new System.Drawing.Size(41, 17);
         this.radioButtonList.TabIndex = 2;
         this.radioButtonList.Text = "List";
         this.radioButtonList.UseVisualStyleBackColor = true;
         this.radioButtonList.CheckedChanged += new System.EventHandler(this.radioButtonList_CheckedChanged);
         // 
         // radioButtonSmallIcon
         // 
         this.radioButtonSmallIcon.AutoSize = true;
         this.radioButtonSmallIcon.Location = new System.Drawing.Point(6, 42);
         this.radioButtonSmallIcon.Name = "radioButtonSmallIcon";
         this.radioButtonSmallIcon.Size = new System.Drawing.Size(74, 17);
         this.radioButtonSmallIcon.TabIndex = 1;
         this.radioButtonSmallIcon.Text = "Small Icon";
         this.radioButtonSmallIcon.UseVisualStyleBackColor = true;
         this.radioButtonSmallIcon.CheckedChanged += new System.EventHandler(this.radioButtonSmallIcon_CheckedChanged);
         // 
         // radioButtonLargeIcon
         // 
         this.radioButtonLargeIcon.AutoSize = true;
         this.radioButtonLargeIcon.Location = new System.Drawing.Point(6, 19);
         this.radioButtonLargeIcon.Name = "radioButtonLargeIcon";
         this.radioButtonLargeIcon.Size = new System.Drawing.Size(76, 17);
         this.radioButtonLargeIcon.TabIndex = 0;
         this.radioButtonLargeIcon.Text = "Large Icon";
         this.radioButtonLargeIcon.UseVisualStyleBackColor = true;
         this.radioButtonLargeIcon.CheckedChanged += new System.EventHandler(this.radioButtonLargeIcon_CheckedChanged);
         // 
         // buttonBack
         // 
         this.buttonBack.Location = new System.Drawing.Point(275, 240);
         this.buttonBack.Name = "buttonBack";
         this.buttonBack.Size = new System.Drawing.Size(75, 23);
         this.buttonBack.TabIndex = 3;
         this.buttonBack.Text = "Back";
         this.buttonBack.UseVisualStyleBackColor = true;
         this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
         // 
         // imageListSmall
         // 
         this.imageListSmall.TransparentColor = System.Drawing.Color.Transparent;
         this.imageListSmall.Images.SetKeyName(0, "Folder 16x16.ICO");
         this.imageListSmall.Images.SetKeyName(1, "Text 16x16.ICO");
         // 
         // imageListLarge
         // 
         this.imageListLarge.TransparentColor = System.Drawing.Color.Transparent;
         this.imageListLarge.Images.SetKeyName(0, "Folder32x32.ICO");
         this.imageListLarge.Images.SetKeyName(1, "Text 32x32.ICO");
         // 
         // Form1
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(564, 273);
         this.Controls.Add(this.buttonBack);
         this.Controls.Add(this.groupBoxViewMode);
         this.Controls.Add(this.listViewFilesAndFolders);
         this.Controls.Add(this.labelCurrentPath);
         this.Name = "Form1";
         this.Text = "ListView";
         this.groupBoxViewMode.ResumeLayout(false);
         this.groupBoxViewMode.PerformLayout();
         this.ResumeLayout(false);

      }

      private System.Windows.Forms.Label labelCurrentPath;
      private System.Windows.Forms.ListView listViewFilesAndFolders;
      private System.Windows.Forms.GroupBox groupBoxViewMode;
      private System.Windows.Forms.RadioButton radioButtonTile;
      private System.Windows.Forms.RadioButton radioButtonDetails;
      private System.Windows.Forms.RadioButton radioButtonList;
      private System.Windows.Forms.RadioButton radioButtonSmallIcon;
      private System.Windows.Forms.RadioButton radioButtonLargeIcon;
      private System.Windows.Forms.Button buttonBack;
      private System.Windows.Forms.ImageList imageListSmall;
      private System.Windows.Forms.ImageList imageListLarge;

      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
      }
   }
23.29.ListView
23.29.1.ListView SorterListView Sorter
23.29.2.Fill data from Database to ListView
23.29.3.ListView Drives
23.29.4.ListView for File and Folder
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.