Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
313

Chapter 13


Exploring Text Files


and String Processing


After completing this chapter, you will be able to:
n Use the My namespace, a time-saving “speed dial” feature within Visual Studio 2010.
n Display text from a file in a text box object by using the ReadAllText method and the
OpenFileDialog control.
n Save notes in a text file by using the WriteAllText method and the SaveFileDialog
control.
n Use string processing techniques in the String class to compare, combine, sort,
and encrypt strings.
Managing electronic documents is an important function in any modern business, and
Microsoft Visual Basic 2010 provides numerous mechanisms for working with different
document types and manipulating the information in documents. The most basic document
type is the text file, which is made up of non-formatted words and paragraphs, letters,
numbers, and a variety of special-purpose characters and symbols.

In this chapter, you’ll learn how to work with information stored in text files on your system.
You’ll learn how to open a text file and display its contents in a text box object, and you’ll
learn how to write to a text file on disk. You’ll also learn more about managing strings in your
programs, and you’ll use methods in the Microsoft .NET Framework String, StreamReader,
and StreamWriter classes to combine; sort; and display words, lines, and entire text files.

Reading Text Files


A text file consists of one or more lines of numbers, words, or characters. Text files are distinct
from document files and Web pages, which contain formatting codes, and from executable
files, which contain instructions for the operating system. Text files on your computer are
typically identified by Windows Explorer as “Text Documents,” or they have the file name
extension .txt, .ini, .log, or .inf.

The simplest way to display a text file in a program is to use a text box object. As you have
learned, you can create text box objects in any size. If the contents of the text file don’t
fit neatly in the text box, you can also add scroll bars to the text box so that the user can
examine the entire file.

Table of Contents


Exploring Text Files
and String Processing............................... 313
Reading Text Files................................................... 313
The My Namespace............................................ 314
The StreamReader Class........................................ 316
Using the ReadAllText Method.................................. 317
Writing Text Files................................................... 321
The WriteAllText Method...................................... 321
The StreamWriter Class........................................ 322
Using the WriteAllText Method................................. 323
Processing Strings with the String Class............................... 326
Sorting Text........................................................ 329
Working with ASCII Codes...................................... 330
Sorting Strings in a Text Box.................................... 331
Examining the Sort Text Program Code........................... 334
Protecting Text with Basic Encryption................................. 336
One Step Further: Using the Xor Operator............................. 340
Examining the Encryption Program Code......................... 342
Chapter 13 Quick Reference.......................................... 345

Free download pdf