Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保留相似的字符编码_C#_Encoding_Text Files - Fatal编程技术网

C# 如何保留相似的字符编码

C# 如何保留相似的字符编码,c#,encoding,text-files,C#,Encoding,Text Files,我有一个包含半个字符½的日志文件,我需要处理该日志文件并将某些行重写为一个新文件,其中包含该字符。但是,当我写出文件时,记事本中的字符显示不正确 我知道这是某种编码问题,我不确定这是否只是因为我正在编写的文件不包含正确的bom或什么 我已尝试使用编码枚举中的所有可用编码选项来读取和写入该文件 我正在使用以下代码: string line; // Note i've used every version of the Encoding enumeration using (StreamReade

我有一个包含半个字符½的日志文件,我需要处理该日志文件并将某些行重写为一个新文件,其中包含该字符。但是,当我写出文件时,记事本中的字符显示不正确

我知道这是某种编码问题,我不确定这是否只是因为我正在编写的文件不包含正确的bom或什么

我已尝试使用编码枚举中的所有可用编码选项来读取和写入该文件

我正在使用以下代码:

string line;

// Note i've used every version of the Encoding enumeration
using (StreamReader sr = new StreamReader(file, Encoding.Unicode))
using (StreamWRiter sw = new StreamWriter(newfile, false, Encoding.Unicode))
{
    while ((line = sr.ReadLine()) != null)
    {
        // process code, I do not alter the lines, they are copied verbatim
        // but i do not write every line that i read.

        sw.WriteLine(line);
    } 
}
当我查看原始登录记事本时,半个字符正确显示。当我查看新文件时,它不会。这告诉我问题不在于记事本能够显示字符,因为它可以在原稿中工作


有人能帮我解决这个问题吗?

解决方案是PEBKAC

我在程序的另一个部分更改了编码,而不是创建这些文件。当我使用Encoding.Default更改了正确的文件时,它显示正确


谢谢Jon和其他人。

编码不是枚举,但有许多静态属性。您是否尝试过编码。默认值?这和Encoding.UTF8是最可能的选项。原始文件是unicode格式的吗?请阅读注释。我都试过了,包括默认值。Jon,UTF8也不行。在记事本中仍然显示为一个奇怪的字符。这可能是因为我正在使用ReadLine()并将其存储在字符串中吗?这会影响编码吗?使用StreamWriter以特定编码打开文件是否会创建BOM表?