Επεξεργασία

Κοινή χρήση μέσω


StringWriter Constructors

Definition

Initializes a new instance of the StringWriter class.

Overloads

StringWriter()

Initializes a new instance of the StringWriter class.

StringWriter(IFormatProvider)

Initializes a new instance of the StringWriter class with the specified format control.

StringWriter(StringBuilder)

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

StringWriter(StringBuilder, IFormatProvider)

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

StringWriter()

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class.

public:
 StringWriter();
public StringWriter ();
Public Sub New ()

Examples

The following code example demonstrates how to construct a string using the StringWriter class.

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   StringWriter^ strWriter = gcnew StringWriter;
   
   // Use the three overloads of the Write method that are 
   // overridden by the StringWriter class.
   strWriter->Write( "file path characters are: " );
   strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
   strWriter->Write( Char::Parse( "." ) );
   
   // Use the underlying StringBuilder for more complex 
   // manipulations of the string.
   strWriter->GetStringBuilder()->Insert( 0, "Invalid " );
   
   Console::WriteLine( "The following string is {0} encoded.\n{1}", strWriter->Encoding->EncodingName, strWriter->ToString() );
   
}
using System;
using System.IO;
using System.Text;

class StrWriter
{
    static void Main()
    {
        StringWriter strWriter  = new StringWriter();

        // Use the three overloads of the Write method that are
        // overridden by the StringWriter class.
        strWriter.Write("file path characters are: ");
        strWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
        strWriter.Write('.');

        // Use the underlying StringBuilder for more complex
        // manipulations of the string.
        strWriter.GetStringBuilder().Insert(0, "Invalid ");

        Console.WriteLine("The following string is {0} encoded.\n{1}",
            strWriter.Encoding.EncodingName, strWriter.ToString());
    }
}
Imports System.IO
Imports System.Text

Public Class StrWriter

    Shared Sub Main()

        Dim strWriter As StringWriter = new StringWriter()

        ' Use the three overloads of the Write method that are 
        ' overridden by the StringWriter class.
        strWriter.Write("file path characters are: ")
        strWriter.Write( _
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)
        strWriter.Write("."C)

        ' Use the underlying StringBuilder for more complex 
        ' manipulations of the string.
        strWriter.GetStringBuilder().Insert(0, "Invalid ")

        Console.WriteLine("The following string is {0} encoded." _
            & vbCrLf & "{1}", _
            strWriter.Encoding.EncodingName, strWriter.ToString())

    End Sub
End Class

Remarks

A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class. Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.

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. How to: Write Text to a File
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
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. 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

See also

Applies to

StringWriter(IFormatProvider)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class with the specified format control.

public:
 StringWriter(IFormatProvider ^ formatProvider);
public StringWriter (IFormatProvider formatProvider);
public StringWriter (IFormatProvider? formatProvider);
new System.IO.StringWriter : IFormatProvider -> System.IO.StringWriter
Public Sub New (formatProvider As IFormatProvider)

Parameters

formatProvider
IFormatProvider

An IFormatProvider object that controls formatting.

Examples

The following code example demonstrates how to construct a string in a specific culture.

using namespace System;
using namespace System::Globalization;
using namespace System::IO;
int main()
{
   StringWriter^ strWriter = gcnew StringWriter( gcnew CultureInfo(  "ar-DZ" ) );
   strWriter->Write( DateTime::Now );
   
   Console::WriteLine( "Current date and time using the invariant culture: {0}\n"
   "Current date and time using the Algerian culture: {1}", DateTime::Now.ToString(), strWriter->ToString() );
   
}
using System;
using System.Globalization;
using System.IO;

class StrWriter
{
    static void Main()
    {
        StringWriter strWriter =
            new StringWriter(new CultureInfo("ar-DZ"));

        strWriter.Write(DateTime.Now);

        Console.WriteLine(
            "Current date and time using the invariant culture: {0}\n" +
            "Current date and time using the Algerian culture: {1}",
            DateTime.Now.ToString(), strWriter.ToString());
    }
}
Imports System.Globalization
Imports System.IO

Public Class StrWriter

    Shared Sub Main()
        Dim strWriter As New StringWriter(New CultureInfo("ar-DZ"))

        strWriter.Write(DateTime.Now)

        Console.WriteLine( _
            "Current date and time using the invariant culture: {0}" _
            & vbCrLf & _
            "Current date and time using the Algerian culture: {1}", _
            DateTime.Now.ToString(), strWriter.ToString())
    End Sub

End Class

Remarks

A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class.

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. How to: Write Text to a File
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
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. 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

See also

Applies to

StringWriter(StringBuilder)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

public:
 StringWriter(System::Text::StringBuilder ^ sb);
public StringWriter (System.Text.StringBuilder sb);
new System.IO.StringWriter : System.Text.StringBuilder -> System.IO.StringWriter
Public Sub New (sb As StringBuilder)

Parameters

sb
StringBuilder

The StringBuilder object to write to.

Exceptions

sb is null.

Examples

The following code example demonstrates using the StringBuilder class to modify the underlying string in a closed StringWriter.

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   StringBuilder^ strBuilder = gcnew StringBuilder( "file path characters are: " );
   StringWriter^ strWriter = gcnew StringWriter( strBuilder );
   strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
   
   strWriter->Close();
   
   // Since the StringWriter is closed, an exception will 
   // be thrown if the Write method is called. However, 
   // the StringBuilder can still manipulate the string.
   strBuilder->Insert( 0, "Invalid " );
   Console::WriteLine( strWriter->ToString() );
   
}
using System;
using System.IO;
using System.Text;

class StrWriter
{
    static void Main()
    {
        StringBuilder strBuilder =
            new StringBuilder("file path characters are: ");
        StringWriter strWriter = new StringWriter(strBuilder);

        strWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        strWriter.Close();

        // Since the StringWriter is closed, an exception will
        // be thrown if the Write method is called. However,
        // the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ");
        Console.WriteLine(strWriter.ToString());
    }
}
Imports System.IO
Imports System.Text

Public Class StrWriter

    Shared Sub Main()
        Dim strBuilder As New StringBuilder( _
            "file path characters are: ")
        Dim strWriter As New StringWriter(strBuilder)

        strWriter.Write( _
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)

        strWriter.Close()

        ' Since the StringWriter is closed, an exception will 
        ' be thrown if the Write method is called. However, 
        ' the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ")
        Console.WriteLine(strWriter.ToString())
    End Sub

End Class

Remarks

Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.

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. How to: Write Text to a File
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
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. 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

See also

Applies to

StringWriter(StringBuilder, IFormatProvider)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

public:
 StringWriter(System::Text::StringBuilder ^ sb, IFormatProvider ^ formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider? formatProvider);
new System.IO.StringWriter : System.Text.StringBuilder * IFormatProvider -> System.IO.StringWriter
Public Sub New (sb As StringBuilder, formatProvider As IFormatProvider)

Parameters

sb
StringBuilder

The StringBuilder object to write to.

formatProvider
IFormatProvider

An IFormatProvider object that controls formatting.

Exceptions

sb is null.

Remarks

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. How to: Write Text to a File
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
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. 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

See also

Applies to