Export (0) Print
Expand All

FileInfo.CreateText Method

Creates a StreamWriter that writes a new text file.

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

public StreamWriter CreateText ()
public StreamWriter CreateText ()
public function CreateText () : StreamWriter

Return Value

A new StreamWriter.

Exception typeCondition

UnauthorizedAccessException

The file name is a directory.

IOException

The disk is read-only.

SecurityException

The caller does not have the required permission.

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

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.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append 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.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Create a directory.

CreateDirectory

Directory

The following example demonstrates the CreateText method.

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);
            }
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        if (!(fi.get_Exists())) {
            //Create a file to write to.
            StreamWriter sw = fi.CreateText();

            try {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
            finally {
                sw.Dispose();
            }
        }

        //Open the file to read from.
        StreamReader sr = fi.OpenText();

        try {
            String s = "";

            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
        finally {
            sr.Dispose();
        }
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft