Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Scripting
Script Runtime
FileSystemObject
 OpenAsTextStream Method
Collapse All/Expand All Collapse All
This page is specific to
.NET Framework 3.0

Other versions are also available for the following:
Scripting Runtime Library
OpenAsTextStream Method

Updated: April 2009

Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.

object.OpenAsTextStream([iomode, [format]]) 
object

Required. Always the name of a File object.

iomode

Optional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.

format

Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

The iomode argument can have any of the following settings:

Constant

Value

Description

ForReading

1

Open a file for reading only. You can't write to this file.

ForWriting

2

Open a file for writing. If a file with the same name exists, its previous contents are overwritten.

ForAppending

8

Open a file and write to the end of the file.

The format argument can have any of the following settings:

Constant

Value

Description

TristateUseDefault

-2

Opens the file using the system default.

TristateTrue

-1

Opens the file as Unicode.

TristateFalse

 0

Opens the file as ASCII.

The OpenAsTextStream method provides the same functionality as the OpenTextFile method of the FileSystemObject. In addition, the OpenAsTextStream method can be used to write to a file.

The following code illustrates the use of the OpenAsTextStream method:

JScript
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;

var fso = new ActiveXObject("Scripting.FileSystemObject");

// Create the file, and obtain a file object for the file.
var filename = "c:\\testfile.txt";
fso.CreateTextFile(filename);
var fileObj = fso.GetFile(filename);

// Open a text stream for output.
var ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault);

// Write to the text stream.
ts.WriteLine("Hello World!");
ts.WriteLine("The quick brown fox");
ts.Close();

// Open a text stream for input.
ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault);

// Read from the text stream and display the results.
while (!ts.AtEndOfStream)
    {
    var textLine = ts.ReadLine();
    document.write (textLine + "<br />");
    }
ts.Close();
VBScript
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, ts, fileObj, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

' Create the file, and obtain a file object for the file.
FileName = "c:\testfile.txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile(FileName)

' Open a text stream for output.
Set ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault)

' Write to the text stream.
ts.WriteLine "Hello World!"
ts.WriteLine "The quick brown fox"
ts.Close

' Open a text stream for input.
Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault)

' Read from the text stream and display the results.
Do While ts.AtEndOfStream <> True
    TextLine = ts.ReadLine
    Document.Write TextLine & "<br />"
Loop

ts.Close

Applies To:

Date

History

Reason

April 2009

Modified examples.

Information enhancement.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker