'VB.Net - Reading, Writing and Appending Stream Files
'
'(c) Jon Vote 07/2002
'Idioma Software Inc.
'All rights reserved.
'
'This code may be freely used and copied
'as long as credit is given to the author.
'
'All code is presented as is.
'User uses at own risk.
'
'Idioma Software Inc. will not
'be responsible for any
'errors contained herein.
'
'Idioma Software Inc.
'www.idioma-software.com
'www.skycoder.com
'jon@skycoder.com
'1) Start a new solution. Form1 will be created by default.
'2) Press F7 to bring up the code window.
'3) Replace ALL (and I mean all!) code in the code window with the following:
'
'Reading and Writing Text Files
'Jon Vote 07/2002
'Idioma Software Inc.
'www.idioma-software.com
'www.skycoder.com
'jon@idioma-software.com
'
'Reading and Writing Text Files
'Jon Vote 07/2002
'Idioma Software Inc.
'www.idioma-software.com
'www.skycoder.com
'jon@idioma-software.com
'
Public Class Form1
Inherits System.Windows.Forms.Form
Private m_strTempFileName As String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnWrite As System.Windows.Forms.Button
Friend WithEvents btnAppend As System.Windows.Forms.Button
Friend WithEvents btnRead As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents txtStreamText As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnWrite = New System.Windows.Forms.Button()
Me.btnAppend = New System.Windows.Forms.Button()
Me.btnRead = New System.Windows.Forms.Button()
Me.txtStreamText = New System.Windows.Forms.TextBox()
Me.btnClose = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnWrite
'
Me.btnWrite.Location = New System.Drawing.Point(10, 168)
Me.btnWrite.Name = "btnWrite"
Me.btnWrite.Size = New System.Drawing.Size(64, 26)
Me.btnWrite.TabIndex = 0
Me.btnWrite.Text = "&Write"
'
'btnAppend
'
Me.btnAppend.Location = New System.Drawing.Point(79, 168)
Me.btnAppend.Name = "btnAppend"
Me.btnAppend.Size = New System.Drawing.Size(64, 26)
Me.btnAppend.TabIndex = 1
Me.btnAppend.Text = "&Append"
'
'btnRead
'
Me.btnRead.Location = New System.Drawing.Point(148, 168)
Me.btnRead.Name = "btnRead"
Me.btnRead.Size = New System.Drawing.Size(64, 26)
Me.btnRead.TabIndex = 2
Me.btnRead.Text = "&Read"
'
'txtStreamText
'
Me.txtStreamText.Location = New System.Drawing.Point(8, 10)
Me.txtStreamText.Multiline = True
Me.txtStreamText.Name = "txtStreamText"
Me.txtStreamText.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtStreamText.Size = New System.Drawing.Size(274, 146)
Me.txtStreamText.TabIndex = 3
Me.txtStreamText.Text = "This is some text."
'
'btnClose
'
Me.btnClose.Location = New System.Drawing.Point(217, 168)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(64, 26)
Me.btnClose.TabIndex = 5
Me.btnClose.Text = "&Close"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 201)
Me.Controls.AddRange(New System.Windows.Forms.Control() _
{Me.btnClose, Me.txtStreamText, Me.btnRead, Me.btnAppend, _
Me.btnWrite})
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "Reading and Writing Text Files"
Me.ResumeLayout(False)
End Sub
#End Region
'Write text to the file.
Private Sub btnWrite_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnWrite.Click
Dim f As Integer
Try
f = FreeFile()
FileOpen(f, GetFilename, OpenMode.Output, OpenAccess.Write)
Print(f, txtStreamText.Text)
FileClose(f)
Catch
MsgBox(Err.Description)
End Try
End Sub
'Append text to the file.
Private Sub btnAppend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAppend.Click
Dim f As Integer
Try
f = FreeFile()
FileOpen(f, GetFilename, OpenMode.Append)
Print(f, txtStreamText.Text)
FileClose(f)
Catch
MsgBox(Err.Description)
End Try
End Sub
'Read text from the file.
Private Sub btnRead_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRead.Click
Dim f As Integer
Dim strinput As String
Try
txtStreamText.Clear()
f = FreeFile()
FileOpen(f, GetFilename, OpenMode.Input, OpenAccess.Read)
Do While Not EOF(f)
txtStreamText.Text &= LineInput(f)
Loop
FileClose(f)
Catch
MsgBox(Err.Description)
End Try
End Sub
'Outta here dude.
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
'Returns filename.
'Prompts user if null.
Private Function GetFilename()
Static strFileName As String
Try
If strFileName = "" Then
strFileName = _
InputBox("Please enter a temp filename. " _
& Microsoft.VisualBasic.vbCrLf _
& "(Be sure the folder exists!)", _
"Temp Filename", _
"c:\temp\temp.txt")
If strFileName <> "" Then
If MsgBox("Ok to use " & _
strFileName & " for this example?", _
MsgBoxStyle.YesNo, "Temp Filename") _
<> MsgBoxResult.Yes Then
strFileName = ""
End If
End If
End If
Finally
GetFilename = strFileName
End Try
End Function
'Form closed
Private Sub Form1_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
End
End Sub
End Class