FileInfo.CreateText Method ()
.NET Framework (current version)
Creates a StreamWriter that writes a new text file.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| 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 example demonstrates the CreateText method.
Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\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 'This code produces output similar to the following; 'results may vary based on the computer/file structure/etc.: ' 'Hello 'AndAnd 'Welcome
FileIOPermission
for reading and writing files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write
Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: