jump to navigation

Manage files,folders, special folders with VB.Net and window forms December 1, 2008

Posted by fofo in general .net, VB 2005, VB 9.0, Visual Studio 2005, Visual Studio 2008.
Tags: , , , , ,
trackback

Hello all !!!

I came across a project today that it was required that we manage files,folders,special folders through windows forms.

Basically that specific functionality was already implemented with scripts. Some of the tasks required were implemented with WMI and some using the Windows Script Host FileSystemObject

If you want to find more scripts like that visit the Script Center

In this post I will show you how to implement file,folder related tasks, by merely using the classes and methods that are already written for us in the FCL in .Net and reside in the System.IO namespace.

I will create an example so we can follow step by step how to accomplish a similar task

1) Launch Visual Studio 2005/2008.

2) Create a new Windows Forms project and choose VB as your development language.

3) Save your project-solution with a name of your choice.

4) Add a reference to the System.IO namespace from your project

5) Add a button and a listbox from the Toolbox on the form. Name the button “FindFiles”

6) In this first example we will see what methods to use to retrieve all files from a folder

7) Double click the button and in the event handling routine that is automatically created type this code

Dim thefiles() As String
‘change the path to a path that exists in your machine
thefiles = Directory.GetFiles(“C:\Acer\Empowering Technology”)
Me.ListBox1.DataSource = thefiles

If you wanted to get files with a specific type e.g all .dll in tha folder you can replace this line

thefiles = Directory.GetFiles(“C:\Acer\Empowering Technology”)

with this

thefiles = Directory.GetFiles(“C:\Acer\Empowering Technology”, “*.dll”)

8)Build your solution and then run your application. Click on the button and see all the files from this particular folder to get listed inside the ListBox control. We use the Directory Class and the GetFiles method of this class.

9) Add another button in the for. Name it “FindFolders”. As you can imagine we will find all the subfolders inside that folder.Double click the button and in the event handling routine that is automatically created type this code

Dim thefolders() As String
thefolders = Directory.GetDirectories(“C:\Acer\Empowering Technology”)
Me.ListBox1.DataSource = thefolders

10) Run your application. Click on the “FindFolders” button and see all the subfolders from this particular folder to get listed inside the ListBox control

11) Let’s see now how we can access special folders like “My Pictures”, “My Documents”.Add another button in your form and name it “GetSpecialFolders”.We will try to get all the files inside the My Documents special folder.

12) Double click the button and in the event handling routine that is automatically created type this code

Dim thefiles() As String
Dim specialfolder As String
specialfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
thefiles = Directory.GetFiles(specialfolder)
Me.ListBox1.DataSource = thefiles

As you can see we use the Environment.SpecialFolder.MyDocuments toget access to that special folder.

13) In this final example I want to show you how to find information like file size for all the files that are inside the “C:\Acer\Empowering Technology” folder. We need to find the file size, the time the file was created and the extension of the file.

In order to to do that we must place 3 textboxes and 3 labels on our form. Change the text property of the first label to “FileSize” .Change the text property of the second label to “File Created” .Change the text property of the third label to “Extension”.

We will place the values we find for each file inside the textboxes.

We will use the SelectedIndexChanged event of the ListBox control to write our code. Just click on the Listbox control from the Form Designer. In the event routine that is created type the following lines.

Dim theselectedfile As String
theselectedfile = Me.ListBox1.SelectedItem.ToString
Dim myfile As New FileInfo(theselectedfile)
Me.TextBox1.Text = myfile.Length.ToString()
Me.TextBox2.Text = myfile.CreationTime.ToString()
Me.TextBox3.Text = myfile.Extension.ToString()

14) Run your application and click on the button “FindFiles”. See the listbox populated with files. Just click on several files and see the results for each file inside the textboxes.

That is all folks!!! Hope it helps.Make sure you explore the other methods and classes in the System.IO namespace.

If you need the source code then you can email me or just leave a comment.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

About these ads

Comments»

1. ricoh sanusi - January 22, 2009

thank you, very helpfully..

2. Christian Pedersen - March 10, 2009

Hi!
Really great tutorial! But like always lol, I encounter a problem, it is the “Directory.GetFiles(specialfolder)” the error is the “Directory” it says:

Name ‘Directory’ is not declared.

Could you send me the source so that I can fix it?

My email: gmlmaster@live.se

Thanks!
//Christian

3. Chris - April 15, 2009

Directory is a member of the System.IO namespace
either import system.io at the top of your vb code behind page, or reference Directory as system.io.directory in the code you’re writing

4. Patrik - September 12, 2009

Hello! Great code!

Is it possible to get only the filename in the listbox, not the entire filepath?

Thank you!

5. fofo - September 12, 2009

Patrik thanks for you positive feedback.

You need to use the “GetFileName” method to retrieve the filename only.

have a look at the link below

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

6. dineshkumar s - May 5, 2011

how to open the form when you click the folder or driver in vb.net. Please send the method if you know.

7. Sridhar - August 23, 2011

Really good article.Can you please send me code,it will helps me alot.Thanks in advance

8. Sridhar - August 23, 2011

Really good article.Can you please send me code,it will helps me alot.Thanks in advance

email: sridhar.marrapu@gmail.com


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,787 other followers

%d bloggers like this: