Dela via


My.Computer.FileSystem.ReadAllText Method

Returns the contents of a text file as a String.

' Usage
Dim value As String = My.Computer.FileSystem.ReadAllText(file)
Dim value As String = My.Computer.FileSystem.ReadAllText(file ,encoding)
' Declaration
Public Function ReadAllText( _
   ByVal file As String _
) As String
' -or-
Public Function ReadAllText( _
   ByVal file As String, _
   ByVal encoding As System.Text.Encoding _
) As String

Parameters

  • file
    String. Name and path of the file to read. Required.

  • encoding
    System.Text.Encoding. Character encoding to use in reading the file. Required. Default is UTF-8.

Return Value

String containing the contents of the file.

Exceptions

The contents of the file may not be what is expected, and methods to read from the file may fail.

The following conditions may cause an exception:

Remarks

The ReadAllText method of the My.Computer.FileSystem object allows you to read from a text file. The contents of the file are returned as a string.

The file encoding can be specified if the contents of the file are in an encoding such as ASCII or UTF-8. If you are reading from a file with extended characters, you need to specify the file encoding.

Do not make decisions about the contents of the file based on the name of the file. For example, the file Form1.vb may not be a Visual Basic source file. Verify all inputs before using the data in your application.

Tasks

The following table lists examples of tasks involving the My.Computer.FileSystem.ReadAllText method.

To

See

Read from a text file

How to: Read From Text Files in Visual Basic

Example

This example reads the contents of Test.txt into a string and then displays it in a message box.

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(reader)

This example reads the contents of the ASCII file Test.txt into a string and then displays it in a message box.

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("C:\test.txt", _
   System.Text.Encoding.ASCII)
MsgBox(reader)

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permission may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Read Text from Files with a StreamReader (Visual Basic)

Troubleshooting: Reading from and Writing to Text Files

Walkthrough: Manipulating Files and Directories in Visual Basic

Concepts

File Encodings

Reference

My.Computer.FileSystem Object

System.Text.Encoding

FileSystem.ReadAllText

StreamReader

Other Resources

Reading from Files in Visual Basic