Manage files,folders, special folders with VB.Net and window forms December 1, 2008
Posted by fofo in VB 2005, VB 9.0, Visual Studio 2005, Visual Studio 2008, general .net.Tags: files, folders, System.IO, VB 2005, VB 9.0, windows forms
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.












thank you, very helpfully..
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
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
Hello! Great code!
Is it possible to get only the filename in the listbox, not the entire filepath?
Thank you!
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