Windows Dev Center

The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

File.CreateText Method

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

[Visual Basic]
Public Shared Function CreateText( _
   ByVal path As String _
) As StreamWriter
public static StreamWriter CreateText(
 string path
public: static StreamWriter* CreateText(
 String* path
public static function CreateText(
   path : String
) : StreamWriter;


The file to be opened for writing.

Return Value

A StreamWriter that writes to the specified file using UTF-8 encoding.


Exception Type Condition
UnauthorizedAccessException The caller does not have the required permission.
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 a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
NotSupportedException path is in an invalid format.


This method is equivalent to StreamWriter(String, Boolean) 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 path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Copy a file. File.Copy


Rename or move a file. File.Move


Delete a file. File.Delete


Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. CreateDirectory



[Visual Basic, C#, C++] The following example creates a file for text writing and reading.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        If File.Exists(path) = False Then
            ' Create a file to write to.
            Dim sw As StreamWriter = File.CreateText(path)
        End If

        ' Open the file to read from.
        Dim sr As StreamReader = File.OpenText(path)
        Do While sr.Peek() >= 0
    End Sub
End Class

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";
        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 = "";
            while ((s = sr.ReadLine()) != null) 

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    if (!File::Exists(path)) {
        // Create a file to write to.
        StreamWriter* sw = File::CreateText(path);
        try {
        } __finally {
            if (sw) __try_cast<IDisposable*>(sw)->Dispose();

    // Open the file to read from.
    StreamReader* sr = File::OpenText(path);
    try {
        String* s = S"";
        while (s = sr->ReadLine()) {
    } __finally {
        if (sr) __try_cast<IDisposable*>(sr)->Dispose();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

© 2015 Microsoft