FileSystem.ReadAllText Método
Definição
Retorna o conteúdo de um arquivo de texto como um String.Returns the contents of a text file as a String.
Sobrecargas
| ReadAllText(String) |
Retorna o conteúdo de um arquivo de texto como um |
| ReadAllText(String, Encoding) |
Retorna o conteúdo de um arquivo de texto como um |
ReadAllText(String)
Retorna o conteúdo de um arquivo de texto como um String.Returns the contents of a text file as a String.
public:
static System::String ^ ReadAllText(System::String ^ file);
public static string ReadAllText (string file);
static member ReadAllText : string -> string
Public Shared Function ReadAllText (file As String) As String
Parâmetros
- file
- String
Nome e caminho do arquivo que será lido.Name and path of the file to read.
Retornos
String que contém o conteúdo do arquivo.String containing the contents of the file.
Exceções
O caminho não é válido por um dos seguintes motivos: é uma cadeia de tamanho zero, contém apenas espaços em branco, contém caracteres inválidos ou é um caminho de dispositivo (começa com \\.\); ele termina com uma barra à direita.The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.
file é Nothing.file is Nothing.
O arquivo não existe.The file does not exist.
O arquivo está sendo usado por outro processo, ou ocorre um erro de E/S.The file is in use by another process, or an I/O error occurs.
O caminho excede o tamanho máximo definido pelo sistema.The path exceeds the system-defined maximum length.
Um nome de arquivo ou de diretório no caminho contém dois-pontos (:) ou está em um formato inválido.A file or directory name in the path contains a colon (:) or is in an invalid format.
Não há memória suficiente para gravar a cadeia de caracteres no buffer.There is not enough memory to write the string to buffer.
O usuário não tem as permissões necessárias para exibir o caminho.The user lacks necessary permissions to view the path.
Exemplos
Este exemplo lê o conteúdo de Test.txt em uma cadeia de caracteres e, em seguida, exibe-o em uma caixa de mensagem.This example reads the contents of Test.txt into a string and then displays it in a message box.
Dim reader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(reader)
Este exemplo lê o conteúdo do arquivo ASCII Test.txt em uma cadeia de caracteres e, em seguida, exibe-o em uma caixa de mensagem.This example reads the contents of the ASCII file Test.txt into a string and then displays it in a message box.
Dim reader = My.Computer.FileSystem.ReadAllText("C:\test.txt",
System.Text.Encoding.ASCII)
MsgBox(reader)
Comentários
O método ReadAllText do objeto My.Computer.FileSystem permite que você leia um arquivo de texto.The ReadAllText method of the My.Computer.FileSystem object allows you to read from a text file. O conteúdo do arquivo é retornado como uma cadeia de caracteres.The contents of the file are returned as a string.
A codificação de arquivo pode ser especificada se o conteúdo do arquivo estiver em uma codificação como ASCII ou UTF-8.The file encoding can be specified if the contents of the file are in an encoding such as ASCII or UTF-8. Se você estiver lendo de um arquivo com caracteres estendidos, precisará especificar a codificação do arquivo usando outra sobrecarga do ReadAllText método.If you are reading from a file with extended characters, you need to specify the file encoding using another overload of the ReadAllText method.
Não tome decisões sobre o conteúdo do arquivo com base no nome do arquivo.Do not make decisions about the contents of the file based on the name of the file. Por exemplo, o arquivo Form1.vb pode não ser um arquivo de código-fonte do Visual Basic.For example, the file Form1.vb may not be a Visual Basic source file. Verifique todas as entradas antes de usar os dados no seu aplicativo.Verify all inputs before using the data in your application.
A tabela a seguir lista um exemplo de uma tarefa que envolve o My.Computer.FileSystem.ReadAllText método.The following table lists an example of a task involving the My.Computer.FileSystem.ReadAllText method.
| ParaTo | ConsulteSee |
|---|---|
| Ler de um arquivo de textoRead from a text file | Como ler a partir de arquivos de texto no Visual BasicHow to: Read From Text Files in Visual Basic |
Confira também
Aplica-se a
ReadAllText(String, Encoding)
Retorna o conteúdo de um arquivo de texto como um String.Returns the contents of a text file as a String.
public:
static System::String ^ ReadAllText(System::String ^ file, System::Text::Encoding ^ encoding);
public static string ReadAllText (string file, System.Text.Encoding encoding);
static member ReadAllText : string * System.Text.Encoding -> string
Public Shared Function ReadAllText (file As String, encoding As Encoding) As String
Parâmetros
- file
- String
Nome e caminho do arquivo que será lido.Name and path of the file to read.
- encoding
- Encoding
A codificação de caracteres a ser usada na leitura do arquivo.Character encoding to use in reading the file. Padrão é UTF-8.Default is UTF-8.
Retornos
String que contém o conteúdo do arquivo.String containing the contents of the file.
Exceções
O caminho não é válido por um dos seguintes motivos: é uma cadeia de tamanho zero, contém apenas espaços em branco, contém caracteres inválidos ou é um caminho de dispositivo (começa com \\.\); ele termina com uma barra à direita.The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.
file é Nothing.file is Nothing.
O arquivo não existe.The file does not exist.
O arquivo está sendo usado por outro processo, ou ocorre um erro de E/S.The file is in use by another process, or an I/O error occurs.
O caminho excede o tamanho máximo definido pelo sistema.The path exceeds the system-defined maximum length.
Um nome de arquivo ou de diretório no caminho contém dois-pontos (:) ou está em um formato inválido.A file or directory name in the path contains a colon (:) or is in an invalid format.
Não há memória suficiente para gravar a cadeia de caracteres no buffer.There is not enough memory to write the string to buffer.
O usuário não tem as permissões necessárias para exibir o caminho.The user lacks necessary permissions to view the path.
Exemplos
Este exemplo lê o conteúdo de Test.txt em uma cadeia de caracteres e, em seguida, exibe-o em uma caixa de mensagem.This example reads the contents of Test.txt into a string and then displays it in a message box.
Dim reader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(reader)
Este exemplo lê o conteúdo do arquivo ASCII Test.txt em uma cadeia de caracteres e, em seguida, exibe-o em uma caixa de mensagem.This example reads the contents of the ASCII file Test.txt into a string and then displays it in a message box.
Dim reader = My.Computer.FileSystem.ReadAllText("C:\test.txt",
System.Text.Encoding.ASCII)
MsgBox(reader)
Comentários
O método ReadAllText do objeto My.Computer.FileSystem permite que você leia um arquivo de texto.The ReadAllText method of the My.Computer.FileSystem object allows you to read from a text file. O conteúdo do arquivo é retornado como uma cadeia de caracteres.The contents of the file are returned as a string.
A codificação de arquivo pode ser especificada se o conteúdo do arquivo estiver em uma codificação como ASCII ou UTF-8.The file encoding can be specified if the contents of the file are in an encoding such as ASCII or UTF-8. Se você estiver lendo de um arquivo com caracteres estendidos, precisará especificar a codificação do arquivo.If you are reading from a file with extended characters, you need to specify the file encoding.
Não tome decisões sobre o conteúdo do arquivo com base no nome do arquivo.Do not make decisions about the contents of the file based on the name of the file. Por exemplo, o arquivo Form1.vb pode não ser um arquivo de código-fonte do Visual Basic.For example, the file Form1.vb may not be a Visual Basic source file. Verifique todas as entradas antes de usar os dados no seu aplicativo.Verify all inputs before using the data in your application.
A tabela a seguir lista um exemplo de uma tarefa que envolve o My.Computer.FileSystem.ReadAllText método.The following table lists an example of a task involving the My.Computer.FileSystem.ReadAllText method.
| ParaTo | ConsulteSee |
|---|---|
| Ler de um arquivo de textoRead from a text file | Como ler a partir de arquivos de texto no Visual BasicHow to: Read From Text Files in Visual Basic |