Export (0) Print
Expand All

File.CreateText Method

Creates or opens a file for writing UTF-8 encoded text.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static StreamWriter CreateText(
	string path


Type: System.String
The file to be opened for writing.

Return Value

Type: System.IO.StreamWriter
A StreamWriter that writes to the specified file using UTF-8 encoding.


The caller does not have the required permission.




path is null.






path is in an invalid format.

This method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten. Additional threads are permitted to read the file while it is open.



The following example creates a file for text writing and reading.

using System;
using System.IO;

class Test 
    public static void Main() 
        if (!File.Exists(path)) 
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path)) 

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path)) 
            string s = "";

.NET Framework


.NET Framework Client Profile

Supported in: 4, 3.5 SP1



Community Additions

© 2014 Microsoft