Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FileInfo.CreateText Method

Creates a StreamWriter that writes a new text file.

[Visual Basic]
Public Function CreateText() As StreamWriter
[C#]
public StreamWriter CreateText();
[C++]
public: StreamWriter* CreateText();
[JScript]
public function CreateText() : StreamWriter;

Return Value

A new StreamWriter.

Exceptions

Exception Type Condition
UnauthorizedAccessException The file name is a directory.
IOException The disk is read-only.
SecurityException The caller does not have the required permission.

Remarks

By default, full read/write access to new files is granted to all users.

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

File.AppendText

FileInfo.AppendText

Copy a file. File.Copy

FileInfo.CopyTo

Rename or move a file. File.Move

FileInfo.MoveTo

Delete a file. File.Delete

FileInfo.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

Directory

Example

[Visual Basic, C#, C++] The following example demonstrates the CreateText method.

[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"
        Dim fi As FileInfo = New FileInfo(path)

        If fi.Exists = False Then
            'Create a file to write to.
            Dim sw As StreamWriter = fi.CreateText()
            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If

        'Open the file to read from.
        Dim sr As StreamReader = fi.OpenText()

        Do While sr.Peek() >= 0
            Console.WriteLine(sr.ReadLine())
        Loop
        sr.Close()
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{

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

        if (!fi.Exists) 
        {
            //Create a file to write to.
            using (StreamWriter sw = fi.CreateText()) 
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }

        //Open the file to read from.
        using (StreamReader sr = fi.OpenText()) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    FileInfo* fi = new FileInfo(path);

    if (!fi->Exists) {
        //Create a file to write to.
        StreamWriter* sw = fi->CreateText();
        try {
            sw->WriteLine(S"Hello");
            sw->WriteLine(S"And");
            sw->WriteLine(S"Welcome");
        } __finally {
            if (sw) __try_cast<IDisposable*>(sw)->Dispose();
        }
    }

    //Open the file to read from.
    StreamReader* sr = fi->OpenText();
    try {
        String* s = S"";
        while (s = sr->ReadLine()) {
            Console::WriteLine(s);
        }
    } __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.

Requirements

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

.NET Framework Security: 

See Also

FileInfo Class | FileInfo Members | System.IO Namespace | StreamWriter | 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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.