如何使用 UTF-8 以外的代码页在 C# 中写出文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/373365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:59:48  来源:igfitidea点击:

How do I write out a text file in C# with a code page other than UTF-8?

c#encodingutf-8iso-8859-1

提问by adeena

I want to write out a text file.

我想写出一个文本文件。

Instead of the default UTF-8, I want to write it encoded as ISO-8859-1 which is code page 28591. I have no idea how to do this...

我想将它编码为 ISO-8859-1,即代码页 28591,而不是默认的 UTF-8。我不知道如何做到这一点......

I'm writing out my file with the following very simple code:

我正在用以下非常简单的代码写出我的文件:

using (StreamWriter sw = File.CreateText(myfilename))
{
    sw.WriteLine("my text...");
    sw.Close();
}

采纳答案by Dave Markle

using System.IO;
using System.Text;

using (StreamWriter sw = new StreamWriter(File.Open(myfilename, FileMode.Create), Encoding.WhateverYouWant))
{    
    sw.WriteLine("my text...");     
}

An alternate way of getting your encoding:

获取编码的另一种方法:

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...");             
}

Check out the docs for the StreamWriter constructor.

查看 StreamWriter 构造函数的文档。

回答by Steven Behnke

Change the Encoding of the stream writer. It's a property.

更改流编写器的编码。这是一个财产。

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.encoding.aspx

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.encoding.aspx

So:

所以:

sw.Encoding = Encoding.GetEncoding(28591);

Prior to writing to the stream.

在写入流之前。

回答by Johann Gerell

Simple!

简单的!

System.IO.File.WriteAllText(path, text, Encoding.GetEncoding(28591));

回答by Tree

Wrap your StreamWriter with FileStream, this way:

用 FileStream 包裹你的 StreamWriter,这样:

string fileName = "test.txt";
string textToAdd = "Example text in file";
Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); //Or any other Encoding

using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
{
    using (StreamWriter writer = new StreamWriter(fs, encoding))
    {
        writer.Write(textToAdd);
    }
}

Look at MSDN

看看MSDN

回答by Manish Joisar

You can have something like this

你可以有这样的东西

 switch (EncodingFormat.Trim().ToLower())
    {
        case "utf-8":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, new UTF8Encoding(false), convertToCSV(result, fileName)));
            break;
        case "utf-8+bom":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, new UTF8Encoding(true), convertToCSV(result, fileName)));
            break;
        case "ISO-8859-1":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, Encoding.GetEncoding("iso-8859-1"), convertToCSV(result, fileName)));
            break;
        case ..............
    }