Mega Code Archive

 
Categories / VB.Net / File Directory
 

FileInfo Open (FileMode, FileAccess) opens a file in the specified mode with read, write, or readwrite access

Imports System Imports System.IO Imports System.Text Public Class Test   Public Shared Sub Main()     Dim path As String = "c:\MyTest.txt"     Dim fi As FileInfo = new FileInfo(path)     Dim fs As FileStream     If fi.Exists = False       fs = fi.Create()       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 = fi.Open(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                 fs.Write(b,0,b.Length)                 Catch e As Exception                 Console.WriteLine("Writing was disallowed, as expected: {0}", e.ToString())             End Try         fs.Close()   End Sub End Class