Switches the current stream's encoding to the new one, reading from the specified character position onward.
[Visual Basic] Overloads Overridable Public Sub SwitchEncoding( _ ByVal enc As Encoding, _ ByVal useCharPos As Integer _ ) [C#] public virtual void SwitchEncoding( Encoding enc, int useCharPos ); [C++] public: virtual void SwitchEncoding( Encoding* enc, int useCharPos ); [JScript] public function SwitchEncoding( enc : Encoding, useCharPos : int );
Exception Type | Condition |
---|---|
ArgumentException | useCharPos is invalid. |
ArgumentNullException | enc is a null reference (in Visual Basic Nothing). |
InvalidOperationException | SwitchEncoding is called after reading past the first character buffer's worth of data. |
SwitchEncoding supports the reading of a few lines of header information in one encoding, and then reading the rest of the document in a different encoding.
SwitchEncoding cannot be called after you have read past the first character buffer's worth of data.
This method re-encodes from the specified character position in the stream onwards. Once a stream encoding has been changed, there is no way to read the previous characters from the StreamReader. The bytes are lost.
Note This will only succeed if the StreamReader has not read more than one buffer's worth of data. You can control this to an extent with the bufferSize parameter to the StreamReader constructor.
StreamReader Class | StreamReader Members | System.IO Namespace | StreamReader.SwitchEncoding Overload List