This application is really basic but is pretty neat to make. This application will be able to browse through files on your computer and upon opening a file will display an output “File Exists” or “File Not Found.” At first the code may seem foreign, but I will add comments that help explain what the code is doing.

Setting Up

If you have not done so already, set up an empty web site. Open visual studio and click on File > New > Project. Expand Other Languages > Visual Basic, select Windows Forms Application, name the file Browse a File and click OK.

Step One

You will see a blank form, let’s add a text box and button to make this application function. Open the toolbox panel, grab a TextBox item and place it on the form. Click on the text box and look in the properties window. Change it’s name to txtFileDirectory, as this text box will represent the file name directory. Now from the toolbox place a Button item on the form. This button will be the button that initiates the browsing for a file. In it’s properties, change the name to btnBrowse and change the text to Browse.


We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

Step Two

Double click on the Browse button to open the visual basic class of the form. By double clicking on the button, we create an event handler called btnBrowse_Click that handles code each time the button is pressed. All of the methods used in this code is from a class called ‘OpenFileDialog’ that prompts the user to open a file. I also took the liberty to add comments above the lines of code that explain what is happening line by line.
Code Block
Form1.vb
The OpenFileDialog class is necessary for this code to work.
Public Class Form1

    Private Sub btnBrowse_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnBrowse.Click
        Dim myFileDlog As New OpenFileDialog()

        'look for files in the c drive
        myFileDlog.InitialDirectory = "c:\"

        'specifies what type of data files to look for
        myFileDlog.Filter = "All Files (*.*)|*.*" & _
            "|Data Files (*.dat)|*.dat"

        'specifies which data type is focused on start up
        myFileDlog.FilterIndex = 2

        'Gets or sets a value indicating whether the dialog box restores the current directory before closing.
        myFileDlog.RestoreDirectory = True

        'seperates message outputs for files found or not found
        If myFileDlog.ShowDialog() = _
            DialogResult.OK Then
            If Dir(myFileDlog.FileName) <> "" Then
                MsgBox("File Exists: " & _
                       myFileDlog.FileName, _
                       MsgBoxStyle.Information)
            Else
                MsgBox("File Not Found", _
                       MsgBoxStyle.Critical)
            End If
        End If

        'Adds the file directory to the text box
        txtFileDirectory.Text = myFileDlog.FileName
    End Sub

End Class

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting


Output


Run the application and click the Browse button. A file browser will open up in the local C drive and if you notice in the extension file next to the file name, we allow extensions of data files or all files. Open a file that exists and a message box will pop up saying “File Exists:” with the file directory next to it, and if you click OK the file directory will appear on the text box in the form.




We chose Server Intellect for its cloud hosting, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

This is a basic example of what can be done when working with file directories. Files can be uploaded and downloaded, so there are quite a bit of possibilities available. First get familiar with this tutorial and then go into the more advanced programming.

Browse a File.zip