Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fs As FileStream
If File.Exists(path) = False Then
fs = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
fs.Write(info, 0, info.Length)
fs.Close()
End If
fs = File.Open(path, FileMode.Open, FileAccess.Read)
Dim b(1024) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
Try
Dim fs2 As FileStream = File.Open(path, FileMode.Open)
fs2.Close()
Catch e As Exception
Console.Write("Opening the file twice is disallowed.")
Console.WriteLine(", as expected: {0}", e.ToString())
End Try
fs.Close()
End Sub
End Class