C# File WriteAllText(String, String)

Description

File WriteAllText(String, String) Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Syntax

File.WriteAllText(String, String) has the following syntax.


public static void WriteAllText(
  string path,
  string contents
)

Parameters

File.WriteAllText(String, String) has the following parameters.

  • path - The file to write to.
  • contents - The string to write to the file.

Returns

File.WriteAllText(String, String) method returns

Example

The following code example demonstrates the use of the WriteAllText method to write text to a file.


using System;/* www .j  av a2s .  c o  m*/
using System.IO;
using System.Text;

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

        string createText = "Hello and java2s.com" + Environment.NewLine;
        File.WriteAllText(path, createText);

        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter