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, Visual Studio 2008, VB 9.0, Visual Studio 2005.
Tags: , , , , ,

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

Writing from a windows form to microsoft word 2007 using VSTO November 29, 2008

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

First of all I want to thank all these people who read my posts and thank them for their encouraging comments.

In the feedack I receive I get some “complaints” that my posts are targeted towards the asp.net side of development. Well you are right. I am more knowledgeable in Asp.Net. But I promise to write more about windows forms in the future, starting from this post. And to all VB guys out there, i will try (starting from this post) to write more samples in VB, which is here to stay…

In this post I would like how easy it is to use Visual Studio Tools for Office. If you have never heard before of VSTO just google it. In a few words it is a component of Visual Studio(since Visual Studio 2005) that provides a robust, .NET-based environment for building business applications using classic Office programs like Word and Excel.  So that means you do not have to learn the specific Office object models, and of course you do not need VBA anymore.

Let’s show VSTO with a simple example. We will have a simple table(1 row and 3 cells) in a word 2007 (.docx) document and fill in the values of these 3 cells from a windows form.

1) Start Visual Studio 2008/2005 project.

2) From the “New Project” window  choose a Office 2007 and from the templates Word 2007 Document. Select VB as the development language.Give a name to your project and Press OK

3) In the next window select “Create a new Document” and give name to your new word document, e.g “mydoc”.

4) Click “OK” to any window that asks you for access to the Office programs

5) If you have done everything right up to this point, you will be ablw to see in your visual studio window a blank word document,

6) Add a table with a single row and 3 cells from the ribbon

7) Add a new item in your project, a windows form and call it “wordform”

8) Add 3 Label controls on the form. Name them, Cell1,Cell2,Cell3.

9) Add 3 textbox controls on the form. Leave the default names

10) Add a button on the form. Leave the default name.

11) In the mydoc.vb(this is a file in my example-if you named your word, inputword, it will be inputword.vb) choose the Document.Open event. This is the empty routine.

Private Sub ThisDocument_Open() Handles Me.Open
End Sub

12) Inside this routine we need to show the form we created in the previous steps. So in the body of the routine, type

Dim myform As New wordform()

13) Now we need to create a procedure (and place it in the same class file) to get the data from the form textboxes and place it in the respective cells. Type this code. It is really simple

Public Sub writeintocells(ByVal cellinput As String, ByVal flag As Integer)
If flag = 1 Then
Globals.ThisDocument.Tables(1).Rows(1).Cells(1).Range.Text = cellinput
ElseIf flag = 2 Then
Globals.ThisDocument.Tables(1).Rows(1).Cells(2).Range.Text = cellinput
Globals.ThisDocument.Tables(1).Rows(1).Cells(3).Range.Text = cellinput
End If
Catch ex As Exception
End Try
End Sub

14) Go to your form. Double Click on the button.In the body event handling routine type

If String.IsNullOrEmpty(TextBox1.Text) Then
MessageBox.Show(“enter a value”)
Globals.ThisDocument.writeintocells(Me.TextBox1.Text, 1)
End If
If String.IsNullOrEmpty(TextBox2.Text) Then
MessageBox.Show(“enter a value”)
Globals.ThisDocument.writeintocells(Me.TextBox2.Text, 2)
End If
If String.IsNullOrEmpty(TextBox3.Text) Then
MessageBox.Show(“enter a value”)
Globals.ThisDocument.writeintocells(Me.TextBox3.Text, 3)
End If

In this routine we access our document and access the procedure that we created in our document. We pass the values from the textboxes to the procedure and the procedure writes these values in the cells in the word document

15)Add another button on your form. Leave the default name. Double Click the button and in the body of the event handling routine type


In this routine we just save and close the document.

16) Build and run your project. Type 3 values in the textboxes in your form and bress Button1.

See the values inserted in the cells. Click Button2 to save and close the document.

Hopefully everything makes sense…

If you need the source code just email me or 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


Get every new post delivered to your Inbox.

Join 2,280 other followers