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

Other versions are also available for the following:
 Collapse AllExpand All        Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: JScript 
ReadAll Method

Updated: September 2009

Reads an entire TextStream file and returns the resulting string.

object.ReadAll( );

The object is always the name of a TextStream object.

For large files, using the ReadAll method wastes memory resources. Other techniques should be used to input a file, such as reading a file line by line.

The following example illustrates the use of the ReadAll method:

JScript
function ReadAllTextFile()
{
    var ForReading = 1, ForWriting = 2;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    // Open the file for output.
    var filename = "c:\\testfile.txt";

    var f = fso.OpenTextFile(filename, ForWriting, true);

    // Write to the file.
    f.Write("Header");
    f.Write("1234567890987654321");
    f.Close();

    // Open the file for input.
    f = fso.OpenTextFile(filename, ForReading);

    // Read from the file.
    if (f.AtEndOfStream)
        return ("");
    else
        return (f.ReadAll());
}
VBScript
Function ReadAllTextFile
    Const ForReading = 1, ForWriting = 2
    Dim fso, MyFile, FileName

    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Open the file for output.   
    FileName = "c:\testfile.txt"
    Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
    
    ' Write to the file.
    MyFile.Write "Header"
    MyFile.Write "1234567890987654321"
    MyFile.Close

    ' Open the file for input.
    Set MyFile = fso.OpenTextFile(FileName, ForReading)

    ' Read from the file.
    If MyFile.AtEndOfStream Then
        ReadAllTextFile = ""
    Else
        ReadAllTextFile = MyFile.ReadAll
    End If
End Function

Date

History

Reason

September 2009

Modified the examples.

Customer feedback.

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