C# File WriteAllText(String, String, Encoding)
Description
File WriteAllText(String, String, Encoding)
Creates
a new file, writes the specified string to the file using the specified encoding,
and then closes the file. If the target file already exists, it is overwritten.
Syntax
File.WriteAllText(String, String, Encoding)
has the following syntax.
public static void WriteAllText(
string path,/* w w w. ja v a 2 s.c o m*/
string contents,
Encoding encoding
)
Parameters
File.WriteAllText(String, String, Encoding)
has the following parameters.
path
- The file to write to.contents
- The string to write to the file.encoding
- The encoding to apply to the string.
Returns
File.WriteAllText(String, String, Encoding)
method returns
Example
The following code example demonstrates the use of the WriteAllText method to write text to a file.
using System;//from w w w. j a v a 2s . co m
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText, Encoding.UTF8);
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}