when i need to implement a console app to read and write the french characters in different document type, such as html, txt, and rtf.
since i uses the  File.ReadAllText method to load the content into Memory, if i use the ReadAllText method without the Encoding parameters. the french content will be unable to maintained. it will cause the output of write operation with some weird characters.
string fileContent = File.ReadAllText(signatureTemplateFileName, Encoding.Default);
after the content was loaded, then the content will be wrtie to a new file based on the file format.
for HTML file, we should use Unicode Encoding
 using (FileStream fs = File.Open(signatureFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter writer = new StreamWriter(fs, Encoding.Unicode))
                {
                    writer.Write(fileContent);
                }
            }
for RTF file, we should use Default Encoding
 using (FileStream fs = File.Open(signatureFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter writer = new StreamWriter(fs, Encoding.Default))
                {
                    writer.Write(fileContent);
                }
            }
for TXT file, we should use Unicode Encoding
 using (FileStream fs = File.Open(signatureFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter writer = new StreamWriter(fs, Encoding.Unicode))
                {
                    writer.Write(fileContent);
                }
            } 
 
 
No comments:
Post a Comment