Biometric Code
Biometric Code
The following sample code uses a StreamReader class to read the System.ini file. The
contents of the file are added to a ListBox control. The try...catch block is used to alert the
program if the file is empty. There are many ways to determine when the end of the file is
reached; this sample uses the Peek method to examine the next line before reading it.
Catch
Me.ListBox1.Items.Add("File is empty")
Finally
reader.Close()
End Try
This sample code uses a StreamWriter class to create and write to a file. If you have an
existing file, you can open it in the same way.
This sample code uses a FileInfo object to access a file's properties. Notepad.exe is used in
this example. The properties appear in a ListBox control.
This sample code uses the Directory and Drive classes to list the logical drives on a system.
For this sample, the results appear in a ListBox control.
List subfolders
This sample code uses the GetDirectories method of the Directory class to get a list of
folders.
Dim dir As String
Me.ListBox1.Items.Clear()
Dim dirs() As String = Directory.GetDirectories(winDir)
For Each dir In dirs
Me.ListBox1.Items.Add(dir)
Next
List files
This sample code uses the GetFiles method of the Directory class to get a list of files.
Many things can go wrong when a user gains access to files. The files may not exist, the files
may be in use, or users may not have rights on the files of folders that they are trying to
access. It is important to consider these possibilities when you write code and to handle the
exceptions that may be generated.
Step-by-step example
1. In Visual Basic 2005 or in Visual Basic .NET, create a new Windows Application. By
default, Form1 is created.
4. Paste the following sample code into the Code-Behind Editor window.
Option Strict On
Imports System.IO
End Sub
End Sub
#End Region
Catch
Me.ListBox1.Items.Add("File is empty")
Finally
reader.Close()
End Try
End Sub
NoteYou must change the code in Visual Basic 2005. By default, Visual Basic creates two
files for the project when you create a Windows Forms project. If the form is named Form1,
the two files that represent the form are named Form1.vb and Form1.Designer.vb. You
wri/te the code in the Form1.vb file. The Windows Forms Designer writes the code in the
Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the
implementation of Form1 into two separate files. This behavior prevents the designer-
generated code from being interspersed with your code.
For more information about the new Visual Basic 2005 language enhancements, visit the
following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx
For more information about partial classes and the Windows Forms Designer, visit the
following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms171843.aspx
5. Press F5 to build and then run the program. Click the buttons to view the different
actions. When you view the sample code, you may want to collapse the area named
Windows Form Designer Generated Code to hide this code.