Skip to content

Instantly share code, notes, and snippets.

@JamesVStone
Created June 12, 2018 09:00
Show Gist options
  • Save JamesVStone/50495b06b99d00362d0a91ec030ef49d to your computer and use it in GitHub Desktop.
Save JamesVStone/50495b06b99d00362d0a91ec030ef49d to your computer and use it in GitHub Desktop.

Working with text files

Text files

A text (txt files) file is a file containing an encoded string which is split into lines The most common encoding for these files is ASCII because it contains all the characters we need and is relatively compact. The reason you may use other encoding ciphers is if you need accents or other special characters.

A simple .txt file is called a sequential file.

Visual basic has a built in function you can use to read and write to text files

System.IO.StreamReader()

Create a StreamReader

A stream reader is a buffering mechanism for the string output. It reads the binary as an ASCII string and loads the characters one by one in to a buffer array and saves itself in RAM to be manipulated.

Here is an example of starting a StreamReader instance

Dim FILE_NAME As string "/home/jstone/{{.ENCYPT_MOUNT.}}//Desktop/test.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)

TextBox.Text = objReader.ReadToEnd

objReader.Close()

In most cases however you will need to verify that the file exists use

If System.IO.File.Exists(FILE_NAME) Than

Writing to a text rile

If System.IO.File.Exists(FILE_NAME) = True Then

  Dim objWriter As New System.IO.StreamWriter('test.txt')

  objWriter.Write(TextBox1.Text)
  objWriter.Close()
End If

Copying

If you need to clone a file in a different location e.g backup

System.IO.File.Copy('SecretPasswords.txt', '/media/Evil_USB/mwahahah.txt')

Moving

System.IO.File.Move('Dodgy Browser history.txt', '~/.trash/dodgy.txt')

Deleting

System.IO.File.Delete('~/.trash/dodgy.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment