我想写出一个文本文件.
而不是默认的UTF-8,我想把它编码为ISO-8859-1编码,这是代码页28591.我不知道如何做到这一点......
我用以下非常简单的代码写出我的文件:
using (StreamWriter sw = File.CreateText(myfilename)) { sw.WriteLine("my text..."); sw.Close(); }
Dave Markle.. 81
using System.IO; using System.Text; using (StreamWriter sw = new StreamWriter(File.Open(myfilename, FileMode.Create), Encoding.WhateverYouWant)) { sw.WriteLine("my text..."); }
获取编码的另一种方法:
using System.IO; using System.Text; using (var sw = new StreamWriter(File.Open(@"c:\myfile.txt", FileMode.CreateNew), Encoding.GetEncoding("iso-8859-1"))) { sw.WriteLine("my text..."); }
查看StreamWriter构造函数的文档.
using System.IO; using System.Text; using (StreamWriter sw = new StreamWriter(File.Open(myfilename, FileMode.Create), Encoding.WhateverYouWant)) { sw.WriteLine("my text..."); }
获取编码的另一种方法:
using System.IO; using System.Text; using (var sw = new StreamWriter(File.Open(@"c:\myfile.txt", FileMode.CreateNew), Encoding.GetEncoding("iso-8859-1"))) { sw.WriteLine("my text..."); }
查看StreamWriter构造函数的文档.
简单!
System.IO.File.WriteAllText(path, text, Encoding.GetEncoding(28591));