FileInfo.Open (FileMode, FileAccess) opens a file in the specified mode with read, write, or read/write access. : FileInfo « File Directory « VB.Net






FileInfo.Open (FileMode, FileAccess) opens a file in the specified mode with read, write, or read/write 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

   
  








Related examples in the same category

1.FileInfo Class provides properties and instance methods for the creation, copying, deletion, moving, and opening of files
2.FileInfo provides properties and instance methods for the creation, copying, deletion, moving, and opening of files
3.Create FileInfo class
4.Use FileInfo.AppendText to create StreamWriter and add text to it
5.FileInfo.CopyTo copies a file
6.Use FileInfo open a file to read
7.FileInfo.CopyTo (String, Boolean) copies a file, allowing the overwriting of an existing file.
8.FileInfo.Create
9.FileInfo.CreateText creates a StreamWriter that writes a new text file.
10.FileInfo.Decrypt decrypts a file that was encrypted using the Encrypt method.
11.FileInfo.Delete deletes a file.
12.FileInfo.Directory Property gets an instance of the parent directory.
13.FileInfo.DirectoryName Property gets a string representing the directory's full path.
14.FileInfo.GetAccessControl gets a FileSecurity object that encapsulates the access control list (ACL) entries
15.FileInfo.IsReadOnly Property gets or sets a value that determines if the current file is read only.
16.FileInfo.Length Property gets the size, in bytes, of the current file.
17.FileInfo.Name Property gets the name of the file.
18.FileInfo.Open opens a file in the specified mode.
19.FileInfo.Open with settings for File Read Write Mode, File Access Mode, File Share Mode
20.FileInfo.OpenRead creates a read-only FileStream.
21.FileInfo.OpenText creates a StreamReader with UTF8 encoding that reads from an existing text file.
22.FileInfo.OpenWrite creates a write-only FileStream.
23.FileInfo.Replace replaces the contents of a file
24.FileSystemInfo Class provides the base class for both FileInfo and DirectoryInfo objects.
25.FileInfo.Length Property tells the size, in bytes, of the current file.