Export (0) Print
Expand All

StreamWriter Constructor (String, Boolean)

.NET Framework 1.1

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

[Visual Basic]
Public Sub New( _
   ByVal path As String, _
   ByVal append As Boolean _
)
[C#]
public StreamWriter(
   string path,
 bool append
);
[C++]
public: StreamWriter(
   String* path,
 bool append
);
[JScript]
public function StreamWriter(
   path : String,
 append : Boolean
);

Parameters

path
The complete file path to write to.
append
Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.

Exceptions

Exception Type Condition
UnauthorizedAccessException Access is denied.
ArgumentException path is empty.
ArgumentNullException path is a null reference (Nothing in Visual Basic).
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.
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.
SecurityException The caller does not have the required permission.

Remarks

This constructor creates a StreamWriter with UTF-8 encoding whose GetPreamble method returns an empty byte array. For additional information, see Encoding.

path can be a file name, including a file on a Universal Naming Convention (UNC) share.

path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.

CAUTION   When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

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...
Create a text file. Writing Text to a File
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

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
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

Example

[Visual Basic, C#, C++] The following example demonstrates the StreamWriter constructors.

[Visual Basic] 
Public Sub CreateTextFile(ByVal FileName As String, _
    ByVal TextToAdd As String)

    Dim Fs As FileStream = New FileStream(FileName, _
        FileMode.CreateNew, FileAccess.Write, FileShare.None)

    Dim SwFromFile As StreamWriter = New StreamWriter(FileName)
    SwFromFile.Write(TextToAdd)
    SwFromFile.Flush()
    SwFromFile.Close()

    Dim SwFromFileStream As StreamWriter = New StreamWriter(Fs)
    SwFromFileStream.Write(TextToAdd)
    SwFromFileStream.Flush()
    SwFromFileStream.Close()

    Dim SwFromFileStreamDefaultEnc As StreamWriter = _
     New StreamWriter(Fs, System.Text.Encoding.Default)
    SwFromFileStreamDefaultEnc.Write(TextToAdd)
    SwFromFileStreamDefaultEnc.Flush()
    SwFromFileStreamDefaultEnc.Close()

    Dim SwFromFileTrue As StreamWriter = _
     New StreamWriter(FileName, True)
    SwFromFileTrue.Write(TextToAdd)
    SwFromFileTrue.Flush()
    SwFromFileTrue.Close()

    Dim SwFromFileTrueUTF8Buffer = _
     New StreamWriter(FileName, _
     True, System.Text.Encoding.UTF8, 512)
    SwFromFileTrueUTF8Buffer.Write(TextToAdd)
    SwFromFileTrueUTF8Buffer.Flush()
    SwFromFileTrueUTF8Buffer.Close()

    Dim SwFromFileTrueUTF8 = _
     New StreamWriter(FileName, True, _
     System.Text.Encoding.UTF8)
    SwFromFileTrueUTF8.Write(TextToAdd)
    SwFromFileTrueUTF8.Flush()
    SwFromFileTrueUTF8.Close()

    Dim SwFromFileStreamUTF8Buffer = _
     New StreamWriter(Fs, System.Text.Encoding.UTF8, 512)
    SwFromFileStreamUTF8Buffer.Write(textToAdd)
    SwFromFileStreamUTF8Buffer.Flush()
    SwFromFileStreamUTF8Buffer.Close()

End Sub

[C#] 
public void CreateTextFile(string fileName, string textToAdd) 
{
    string logFile = DateTime.Now.ToShortDateString()
        .Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
    
    FileStream fs = new FileStream(fileName,
        FileMode.CreateNew, FileAccess.Write, FileShare.None);
    
    StreamWriter swFromFile = new StreamWriter(logFile);
    swFromFile.Write(textToAdd);
    swFromFile.Flush();
    swFromFile.Close();

    StreamWriter swFromFileStream = new StreamWriter(fs);
    swFromFileStream.Write(textToAdd);
    swFromFileStream.Flush();
    swFromFileStream.Close();

    StreamWriter swFromFileStreamDefaultEnc = 
        new System.IO.StreamWriter(fs, 
        System.Text.Encoding.Default);
    swFromFileStreamDefaultEnc.Write(textToAdd);
    swFromFileStreamDefaultEnc.Flush();
    swFromFileStreamDefaultEnc.Close();

    StreamWriter swFromFileTrue = 
        new StreamWriter(fileName,true);
    swFromFileTrue.Write(textToAdd);
    swFromFileTrue.Flush();
    swFromFileTrue.Close();
    
    StreamWriter swFromFileTrueUTF8Buffer = 
        new StreamWriter(fileName, 
        true, System.Text.Encoding.UTF8,512);
    swFromFileTrueUTF8Buffer.Write(textToAdd);
    swFromFileTrueUTF8Buffer.Flush();
    swFromFileTrueUTF8Buffer.Close();

    StreamWriter swFromFileTrueUTF8 = 
        new StreamWriter(fileName, true,
        System.Text.Encoding.UTF8);
    swFromFileTrueUTF8.Write(textToAdd);
    swFromFileTrueUTF8.Flush();
    swFromFileTrueUTF8.Close();

    StreamWriter swFromFileStreamUTF8Buffer = 
        new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
    swFromFileStreamUTF8Buffer.Write(textToAdd);
    swFromFileStreamUTF8Buffer.Flush();
    swFromFileStreamUTF8Buffer.Close();
}

[C++] 
public:
   void CreateTextFile(String* fileName, String* textToAdd) 
   {
      String* logFile = String::Concat(
         DateTime::Now.ToShortDateString()->Replace(S"/",S"-")->Replace(S"\\",S"-"),
         S".log" );

      FileStream* fs =
         new FileStream(fileName, FileMode::CreateNew, FileAccess::Write, FileShare::None);

      StreamWriter* swFromFile = new StreamWriter(logFile);
      swFromFile->Write(textToAdd);
      swFromFile->Flush();
      swFromFile->Close();

      StreamWriter* swFromFileStream = new StreamWriter(fs);
      swFromFileStream->Write(textToAdd);
      swFromFileStream->Flush();
      swFromFileStream->Close();

      StreamWriter* swFromFileStreamDefaultEnc = 
         new System::IO::StreamWriter(fs, System::Text::Encoding::Default);
      swFromFileStreamDefaultEnc->Write(textToAdd);
      swFromFileStreamDefaultEnc->Flush();
      swFromFileStreamDefaultEnc->Close();

      StreamWriter* swFromFileTrue = new StreamWriter(fileName,true);
      swFromFileTrue->Write(textToAdd);
      swFromFileTrue->Flush();
      swFromFileTrue->Close();

      StreamWriter* swFromFileTrueUTF8Buffer = 
         new StreamWriter(fileName, true, System::Text::Encoding::UTF8,512);
      swFromFileTrueUTF8Buffer->Write(textToAdd);
      swFromFileTrueUTF8Buffer->Flush();
      swFromFileTrueUTF8Buffer->Close();

      StreamWriter* swFromFileTrueUTF8 = 
         new StreamWriter(fileName, true,System::Text::Encoding::UTF8);
      swFromFileTrueUTF8->Write(textToAdd);
      swFromFileTrueUTF8->Flush();
      swFromFileTrueUTF8->Close();

      StreamWriter* swFromFileStreamUTF8Buffer = 
         new StreamWriter(fs, System::Text::Encoding::UTF8, 512);
      swFromFileStreamUTF8Buffer->Write(textToAdd);
      swFromFileStreamUTF8Buffer->Flush();
      swFromFileStreamUTF8Buffer->Close();
   }

[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, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

StreamWriter Class | StreamWriter Members | System.IO Namespace | StreamWriter Constructor Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft