File Processing
Open a text file for input
Files that can be opened are files with the
extensions .txt, ini, log or inf. These files have no embedded
formatting or other unrecognizable characters.
Syntax:
FileOpen(filenumber, pathname,
mode)
Filenumber is 1 through 255, you use this number
when you write to the file and when you close
Pathname is a valid Windows path, including the
filename and extension
Mode is a keyword indicating how to use the file.
Input means data input to the current VB application.
Example:
fileOpen(1, file.txt, input)
Example using the OpenFileDialog window
fileOpen(1, OpenFileDialog1.FileName,
OpenMode.Input)
Important Functions For Working With Files
FileOpen – opens the file
LineInput – reads one line from the input file
EOF – a check for the end of file marker
FileClose – closes the text file
Writing a Text File
Examples using the open dialog window:
open the file
FileOpen(1,SaveFileDialog1.FileName, OpenMode.Output)
write a line to the file
Printline(1,txtNote.Text)
Close the file
FileClose(1)
NICE Routine!
Uses a menu to collect the Opens File command.
Uses the OpenFileDialog to allow the user to select the file. Makes
use of Try..Catch to trap file errors. Has a comprehensive set of
Reads the file and displays it in a textbox. Just a really nice little
application.
Private
Sub OpenToolStripMenuItem_Click _
(ByVal
sender As System.Object, _
ByVal e As
System.EventArgs) _
Handles
OpenToolStripMenuItem.Click
Dim
AllText As String = "", LineOfText As String = ""
OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"
OpenFileDialog1.ShowDialog() 'display Open dialog box
If
OpenFileDialog1.FileName <> "" Then
Try 'open file and trap any errors using handler
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1) 'read lines from file
LineOfText = LineInput(1)
'add each line to the AllText variable
AllText = AllText & LineOfText & vbCrLf
Loop
lblNote.Text = OpenFileDialog1.FileName 'update label
txtNote.Text = AllText 'display file
txtNote.Enabled = True 'allow text cursor
CloseToolStripMenuItem.Enabled = True
'enable
Close command
OpenToolStripMenuItem.Enabled = False
'disable
Open command
Catch
MsgBox("Error opening file.")
Finally
FileClose(1) 'close file
End Try
End
If
End Sub
|