如何写出有code比UTF-8其他网页在C#中的文本文件? [英] How to write out a text file in C# with a code page other than utf-8?

查看:97
本文介绍了如何写出有code比UTF-8其他网页在C#中的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要写出一个文本文件。

而不是默认的UTF-8的,我想把它写的EN $ C $光盘作为ISO-8859-1是code页面28591.我不知道如何做到这一点...

我在写我的文件,下面很简单code:

 使用(StreamWriter的SW = File.CreateText(mYfILEname的))
{    sw.WriteLine(我的文字......);
    sw.Close();
}


解决方案

 使用System.IO;
使用System.Text;使用(StreamWriter的SW =新的StreamWriter(File.Open(mYfILEname的,FileMode.Create),Encoding.WhateverYouWant))
{
    sw.WriteLine(我的文字......);
}

让你的编码的另一种方法:

 使用System.IO;
使用System.Text;使用(VAR SW =新的StreamWriter(File.Open(@C:\\ myfile.txt的,FileMode.CreateNew),Encoding.GetEncoding(ISO-8859-1))){
    sw.WriteLine(我的文字......);
}

检查出文档的StreamWriter的构造函数。

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...

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

using (StreamWriter sw = File.CreateText(myfilename))
{

    sw.WriteLine("my text...");
    sw.Close();
}

解决方案

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.

这篇关于如何写出有code比UTF-8其他网页在C#中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆