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.
using namespace System; using namespace System::IO; int main() { String^ path = "c:\\MyTest.txt"; FileInfo^ fi = gcnew FileInfo( path ); if ( !fi->Exists ) { //Create a file to write to. StreamWriter^ sw = fi->CreateText(); try { sw->WriteLine( "Hello" ); sw->WriteLine( "And" ); sw->WriteLine( "Welcome" ); } finally { if ( sw ) delete (IDisposable^)sw; } } //Open the file to read from. StreamReader^ sr = fi->OpenText(); try { String^ s = ""; while ( s = sr->ReadLine() ) { Console::WriteLine( s ); } } finally { if ( sr ) delete (IDisposable^)sr; } } //This code produces output similar to the following; //results may vary based on the computer/file structure/etc.: // //Hello //And //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: