JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Opening or Creating a File with the FileStream Class

When you want to open or create a file, you use the FileStream class, which has many constructors, allowing you to specify the file mode (for example, FileMode.Create), file access (such as FileAccess.Write), and/or the file-sharing mode (such as FileShare.None), like this (these are only a few of the FileStream constructors):

Dim fs As New System.IO.FileStream(String, FileMode)
Dim fs As New System.IO.FileStream(String, FileMode, FileAccess)
Dim fs As New System.IO.FileStream(String, FileMode, FileAccess, FileShare)

The StreamWriterReader example on the CD-ROM shows how this works—in that example, I'm creating a file named file.txt and opening it for writing with a FileStream object; note that I'm setting the file mode to Create to create this new file, and explicitly setting the file access to Write so we can write to the file:

Imports System
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim fs As New System.IO.FileStream("file.txt", FileMode.Create, _
            FileAccess.Write)
        

To actually do something with this new FileStream object, I'll use the StreamWriter class, coming up next.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor