Custom Search

Friday, November 28, 2008

Writing File Using VB.Net

Using File stream in VB.Net (clsFile)



Imports System.IO
Public Class clsFiles
Private myBoolVar As Boolean
Property isDirty() As Boolean
'returns or sets the isdirty property
Get
Return myBoolVar
End Get
Set(ByVal Value As Boolean)
myBoolVar = Value
End Set
End Property
Public Function openFile(ByVal path As String) As String
'opens a text file and returns the contents as a string
Dim fdata As String
Try
Dim fstream As System.IO.StreamReader
fstream = New StreamReader(path)
fdata = fstream.ReadToEnd()
fstream.Close()
Return fdata
Catch errors As Exception
MsgBox(errors.Message, MsgBoxStyle.Exclamation, "Error!")
End Try
End Function
Public Function saveFile(ByVal strData As String, ByVal path As String) As Boolean
'saves data passed to it to a file specified by the path variable
'returns true if all goes well
'returns false if $#!? hits the fan
Try
Dim fstream As StreamWriter
Dim Contents As String
fstream = New StreamWriter(path)
fstream.Write(strData)
fstream.Close()
Return True
Catch errors As Exception
MsgBox(errors.Message, MsgBoxStyle.Exclamation, "Error!")
Return False
End Try
End Function
End Class

No comments: