Files in Visual Basic

The code is fairly straightforward. We can write the whole thing in form load:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
   Handles MyBase.Load
  Dim OldFile As String = "C:/states.txt"
  Dim NewFile As String = OldFile.Replace(".txt", ".xml")
  FileOpen(1, OldFile, OpenMode.Input) 'Open for input
  FileOpen(2, NewFile, OpenMode.Output) 'Open for output
  Dim S As String = ""
  Dim Key As String
  Dim Value As String
  PrintLine(2, "<file>")
  PrintLine(2, "<?xml version="1.0" encoding="utf-8"?>")
  While Not EOF(1)
    Input(1, S)
    Key = S.Substring(0, 2) 'we know that the key is the first 2 characters
    Value = S.Substring(3) 'the name of the state begins in position 3
    PrintLine(2, "<" & Key & ">" & Value & "</" & Key & ">")
  End While
  PrintLine(2, "</file>") 'write the ending tag for the xml file
End Sub
If we had a lot of files with a fixed format, we could add an openfile dialog to the program. In the next example, we will use the fileopen dialog control to select a directory, then create a list of all of the files in the directory. If we wished, we could convert all of the files in a directory to XML format, or do hundreds of other thins with them.

NEXT: A List Manager application in Visual Basic