Browse DevX
Sign up for e-mail newsletters from DevX


VB.NET Faces Off Against Classic VB : Page 11

VB.NET, the .NET Framework, and Visual Studio.NET together form a potent combination that can simplify your code and reduce errors; however, to truly supplant existing versions of VB as a RAD tool, Visual Studio needs Break-Edit-Continue.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Read the contents of a text file
File IO has changed considerably in .NET. If you have been using the FileSystemObject to read and write text files, you'll feel comfortable quickly. If you've been using the intrinsic VB commands, such as Open, Get, and Put, file IO may seem strange at first. The example uses the intrinsic VB commands.

Both the VB and VB.NET versions perform the same tasks: retrieve the Windows folder path from the SystemRoot environment variable, ensure that the Win.ini file exists in the Windows folder, then open and read the contents of the file, and assign the resulting string to a multiline TextBox control. You can read the file with a single command in both versions, but the VB.NET version is much more intuitive. For example, it's obvious what the "File.Exists" does, but not nearly as obvious what the Dir$ command does.

Classic VB

   Private Sub Command1_Click()
      Dim fnum As Integer
      Dim s As String
      Dim fname As String
      Dim winPath As String
      On error goto ErrReadTextFile
      fnum = FreeFile
      ' get the windows folder name
      winPath = Environ$("SystemRoot")
      If winPath = "" Then
         MsgBox "Unable to retrieve the Windows path.", _
            vbInformation, "Error Reading Windows Path"
         Exit Sub
      End If
      ' create a file name
      fname = winPath & "\win.ini"
      ' ensure the file exists
      If Dir$(fname) <> "" Then
         ' open the file
         Open fname For Binary As #fnum
         If Err.Number = 0 Then
            s = Space$(LOF(fnum))
            ' read the file
            Get #fnum, 1, s
            Close #fnum
            Text1.Text = s
            Text1 = "Unable to read the file " & _
            fname & "."
         End If
         Text1 = "The file " & fname & " does not exist."
      End If
      Exit Sub
      MsgBox Err.Number & ": " & Err.Description
      Exit Sub
   End SubEnd Sub


   Private Sub btnReadTextFile_Click(ByVal sender _
      As System.Object, ByVal e As System.EventArgs) _
      Handles btnReadTextFile.Click
   Dim winPath As String
   Dim s As String
   Dim fname As String
   Dim sr As StreamReader
   Dim ex As Exception
      ' get the windows folder name
   winPath = System.Environment. _
      If winPath = "" Then
         MessageBox.Show("Unable to retrieve the " & _
        "Windows path.", "Error Reading Windows Path", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)
      End If
      fname = winPath & "\win.ini"
      If File.Exists(fname) Then
            sr = File.OpenText(fname)
            s = sr.ReadToEnd
            textbox1.text = s
         Catch ex
         End Try
         MessageBox.Show("The file " & fname & _
            " does not exist.")
      End If
   End Sub
You can't point to just one or two things that make VB.NET easier to work with—it's the little things that make VB.NET and Visual Studio easier than previous versions. Sometimes, VS.NET requires less code; sometimes the long object names and numerous objects create more code. But after you learn a little about the framework, it almost always creates code that's easier to understand and maintain.

A. Russell Jones, Ph.D., is the Executive Editor at DevX. You can reach him at rjones@devx.com
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date