Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original
Este tópico ainda não foi avaliado como - Avalie este tópico

File.WriteAllLines Método (String, String[], Encoding)

Cria um novo arquivo, grava a matriz de Cadeia de Caracteres especificado para o arquivo usando a codificação especificada e, em seguida, fecha o arquivo.Se o arquivo de destino já existir, ele será sobrescrito.

Namespace:  System.IO
Assembly:  mscorlib (em mscorlib. dll)
public static void WriteAllLines(
	string path,
	string[] contents,
	Encoding encoding
)

Parâmetros

path
Tipo: System.String
O arquivo para gravar.
contents
Tipo: System.String[]
A matriz de Cadeia de Caracteres para gravar o arquivo.
encoding
Tipo: System.Text.Encoding
An Encoding object that represents the character encoding applied to the string array.
ExceçãoCondição
ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException

path is null or contents string is empty.

PathTooLongException

The specified, Name , or both Exceed the Sistema-defined Maximum length.For exemplo, on platforms Windows-Based, Paths must be Less than 248 Caracteres, and names arquivo must be Less than 260 Caracteres.

DirectoryNotFoundException

O caminho especificado é inválido (por exemplo, ele está em uma unidade não mapeada).

IOException

Erro de E/s ao Abrindo o arquivo.

UnauthorizedAccessException

path specified a file that is read-only.

- ou -

Não há suporte para esta operação a plataforma atual.

- ou -

path specified a directory.

- ou -

O chamador não tem a permissão necessária.

FileNotFoundException

The file specified in path was not found.

NotSupportedException

path is in an invalid format.

SecurityException

O chamador não tem a permissão necessária.

A matriz Cadeia de Caracteres given and a Caminho do Arquivo, this método the specified arquivo opens, writes the matriz Cadeia de Caracteres to the arquivo using the specified codificação, and then the arquivo closes.

The following code example demonstrates the use of the WriteAllLines method to write text to a file.Neste exemplo um arquivo é criado, se ainda não existir, e o texto é adicionado a ele.

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}


Isso foi útil para você?
(1500 caracteres restantes)

Contribuições da comunidade

ADICIONAR
© 2013 Microsoft. Todos os direitos reservados.