Windows Develop Bookmark and Share   
 index > Windows Forms General > how do i count files in a folder and pass it to a listview
 

how do i count files in a folder and pass it to a listview

Hi, i have a big question.

how doi count the files in a folder and get that folders name andthe corrent time and pass it to my listview1, like subitems.And then move the folder to another denstination??

Just Need to get started..

It would help me allot if you wrote some codes and exampels...

André Dani  Sunday, August 26, 2007 9:03 PM

For manipulating a directory, see the DirectoryInfo class in System.IO. The MoveTo method will move the directory, while GetFiles and GetDirectories will give you arrays of FileInfo and DirectoryInfo instances. The lengths of these arrays will tell you how many files or folders are contained in the parent folder. This seems like a relatively expensive way of counting files but I haven't found anything better.

http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx

For manipulating dates and times, use the DateTime class. The properrty DateTime.Now returns a DateTime object for the current date and time when Now is accessed..

http://msdn2.microsoft.com/en-us/library/system.datetime.aspx

To add items to a ListView, create a ListViewItem to represent the top level item you are adding, call item.SubItems.Add to add sub items to that item and then add that item to the ListView

http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.aspx

Frank Boyne  Sunday, August 26, 2007 10:55 PM

A simple sample for you

Code Snippet

partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

//count files in a folder

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\test");

int filesCouunt = dir.GetFiles().Length;

this.label1.Text = "There're " + filesCouunt.ToString() + " files" ;

//pass folder name and current time to listview

string name = dir.Name;

DateTime time = DateTime.Now;

ListViewItem item = new ListViewItem(new string[]{name,time.ToString()});

this.listView1.Items.Add(item);

this.listView1.Columns.Add("directory name");

this.listView1.Columns.Add("time");

this.listView1.View = View.Details;

}

private void button1_Click(object sender, EventArgs e)

{

//move folder to another destination

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\test");

dir.MoveTo(@"c:\test2");

}

}

For manipulating a directory, see the DirectoryInfo class in System.IO. The MoveTo method will move the directory, while GetFiles and GetDirectories will give you arrays of FileInfo and DirectoryInfo instances. The lengths of these arrays will tell you how many files or folders are contained in the parent folder. This seems like a relatively expensive way of counting files but I haven't found anything better.

http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx

For manipulating dates and times, use the DateTime class. The properrty DateTime.Now returns a DateTime object for the current date and time when Now is accessed..

http://msdn2.microsoft.com/en-us/library/system.datetime.aspx

To add items to a ListView, create a ListViewItem to represent the top level item you are adding, call item.SubItems.Add to add sub items to that item and then add that item to the ListView

http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.aspx

Frank Boyne  Sunday, August 26, 2007 10:55 PM

A simple sample for you

Code Snippet

partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

//count files in a folder

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\test");

int filesCouunt = dir.GetFiles().Length;

this.label1.Text = "There're " + filesCouunt.ToString() + " files" ;

//pass folder name and current time to listview

string name = dir.Name;

DateTime time = DateTime.Now;

ListViewItem item = new ListViewItem(new string[]{name,time.ToString()});

this.listView1.Items.Add(item);

this.listView1.Columns.Add("directory name");

this.listView1.Columns.Add("time");

this.listView1.View = View.Details;

}

private void button1_Click(object sender, EventArgs e)

{

//move folder to another destination

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\test");

dir.MoveTo(@"c:\test2");

}

}

Thanks man you are the best..! Its a good start.. I need to get the filescount also to the listview1 with the time and name.. And i have a question, the path are saved by a user in an .txt file in row 2. how do i collect the path in to this code.. and it can be more than one path..?

it would help me allot...!!

André Dani  Tuesday, August 28, 2007 4:20 PM

You can try something like this, put attention to the comments

Code Snippet

void Form3_Load(object sender, EventArgs e)

{

//read content from the txt file

string[] lines = System.IO.File.ReadAllLines(@"c:\path.txt");

//retrieve file path from the second row

string path = lines[1];

//count files in a folder

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);

int filesCouunt = dir.GetFiles().Length;

this.label1.Text = "There're " + filesCouunt.ToString() + " files" ;

//pass folder name and current time to listview

string name = dir.Name;

DateTime time = DateTime.Now;

ListViewItem item = new ListViewItem(new string[]{

name,time.ToString(),filesCouunt.ToString()});

this.listView1.Items.Add(item);

this.listView1.Columns.Add("directory name");

this.listView1.Columns.Add("time");

this.listView1.Columns.Add("file count");

this.listView1.View = View.Details;

this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

}


André Dani wrote:

and it can be more than one path..?



what do you mean of this?

Zhi-Xin Ye  Wednesday, August 29, 2007 4:08 AM

that it can be more than one path added by user in the .txt... for example (line[1] after every * in .txt file)?

André Dani  Thursday, August 30, 2007 4:48 PM

You can use google to search for other answers

Custom Search

More Threads

• Problem about using parameters in the delegate under cross-threads working environment.
• Expose ComboBox.Items property outside UserControl?
• changing location of object
• Need Information about writing a custom SettingsProvider.
• Divide et Impera (GraphicsPath)
• Form Text Objects showing wrong color when program runs.
• Moving images fluently
• Problem with LoadImage API in vb
• Take BackUP Of Acess Databse
• Adding form to a panel