Welcome   |  ASP.NET   |  Web Services   |  How Do I...?   |  Class Browser  | WPF Quick Starts
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

How Do I...Use different text encodings?



This sample illustrates how to use multiple encoding formats available from the System.Text namespace. A StreamWriter object is created and passed a Stream object along with an Encoding object from the System.Text namespace. Once created, any output method calls on the StreamWriter object will be written in the specified encoding.

		
FileStream fs = new FileStream("text.txt", FileMode.OpenOrCreate);
 ...
StreamWriter t = new StreamWriter (fs, Encoding.UTF8);
t.Write("This is in UTF8");
C#


A StreamReader object is used to read text in a given encoding and is constructed in the same manner as a StreamWriter object.

		
FileStream fs = new FileStream("text.txt", FileMode.Open);
 ...
StreamReader t = new StreamReader(fs, Encoding.UTF8);
String s = t.ReadLine();
C#


The following example illustrates creating StreamWriter objects of different encodings and using each object to write to a text file.

Example

C# Encoding.exe
View Source

[This sample can be found at M:\web\users\sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\globalization\encoding\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2010  Mindcracker LLC. All Rights Reserved