Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

August 1, 2007

Writing utf-8 File with and without Preamble in C#

Here’s how to control whether or not the utf-8 preamble is included in your file I/O when writing strings to a file (in C#):

    string s = "This is a test éééé";
	
    // Write a string as utf-8 to a file without the 3-byte utf-8 preamble.
    System.Text.Encoding utf8EncoderNoBOM = new System.Text.UTF8Encoding(false);
	
    bool appendMode = false;	// This overwrites the entire file.
    StreamWriter sw = new StreamWriter("out_utf8_noPreamble.txt",
        appendMode,utf8EncoderNoBOM);
    sw.Write(s);
    sw.Close();
	
    // Write the file with the utf-8 preamble:
    StreamWriter sw2 = new StreamWriter("out_utf8_withPreamble.txt",
        appendMode, System.Text.Encoding.UTF8);
    sw2.Write(s);
    sw2.Close();


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.