Saturday, November 15, 2008

FILE HANDLIND IN .NET

CREATING A FILE

Dim oFile As New IO.FileInfo("serverName.txt")
oFile.Create()

DELETING A FILE

Dim oFile As New IO.FileInfo("serverName.txt")
OFILE.DELETE()

WRITING TO FILE

Method 1:

Dim fr As New System.IO.StreamWriter("path as string")
fr.Write("value as string")
fr.Close()

Method 2:

Dim oFile As New IO.FileInfo("serverName.txt")
Dim fs As IO.FileStream

fs = oFile.Create()
Dim info As Byte() = New System.Text.UTF8Encoding(True).GetBytes("This is some text in the file.")

' Add some information to the file.
fs.Write(info, 0, info.Length)
fs.Close()


READING FROM FILE

Method 1:

Dim fr As New System.IO.StreamReader( "serverName.txt")
Dim conStr As String = ""
conStr = fr.ReadLine()

Method 2:

            Dim sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()

     

No comments: