如何用C#从.txt文件中读取西里尔字符 [英] How to read Cyrillic symbols from a .txt file with C#

查看:130
本文介绍了如何用C#从.txt文件中读取西里尔字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到类似的主题,但找不到解决方案。我的问题是我有一个.txt文件,其中的符号是保加利亚语/这是西里尔文/,但是在尝试阅读它们之后,没有成功。我试图用这段代码阅读:

  StreamReader reader = new StreamReader(fileName,Encoding.UTF8); 

if(File.Exists(fileName))
{
while((line = reader.ReadLine())!= null)
{
Console.WriteLine(线);
}
}

而且我也将Encoding值更改为所有可能,当我试图用GetEncoding(1251),我写的是西里尔文。而当我保存.txt文件时,我尝试保存它与每个不同的编码,那里有/ UNICODE,UTF-8,BigEndianUnicode,ANSI /每个组合与编码我通过代码设置,但再次没有成功。 / p>

任何关于如何以正确方式读取西里尔字符符号的想法将被忽略。
这里是这样的示例文本:Етопримерентекст。



提前感谢! :)

解决方案

您的问题是控制台无法显示西里尔字符。尝试在 Console.WriteLine 上放置一个断点,并检查变量。很明显,你需要先知道正确的编码! : - )



如果您不信任我,请尝试以下操作:制作一个控制台程序:

  string line =Етопримерентекст; 
Console.WriteLine(line);
return 0;

return 0上放置一个断点; ,请观看控制台并观看该行变量。



我将补充说,unicode控制台应该是.NET 4.5中的新事物之一



您可以尝试阅读此页面: c#unicode string输出


I saw similar topics but could not find a solution. My problem is that I have a .txt file in which the symbols are in Bulgarian language / which is Cyrillic /, but after trying to read them, there is no sucess. I tried to read with this code:

StreamReader reader = new StreamReader(fileName,Encoding.UTF8);

if (File.Exists(fileName))
{
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

And I also changed the Encoding value to all possible , as I tried with GetEncoding(1251), which I wrote is for cyrillic. And when I save the .txt file I tried to save it with each different encoding which was there / UNICODE,UTF-8,BigEndianUnicode,ANSI / in each combination with the Encoding I am settin through the code, but again no success.

Any ideas for how to read the cyrillic symbols in the right way will be appriciated. And here is sample text for this: "Ето примерен текст."

Thanks in advance! :)

解决方案

Your problem is that the console can't show cyrillic characters. Try putting a breakpoint on the Console.WriteLine and inspect the line variable. Clearly you'll need to know the correct encoding first! :-)

If you don't trust me, try this: make a console program that does this:

string line = "Ето примерен текст"; 
Console.WriteLine(line);
return 0;

put a breakpoint on the return 0;, watch the console and watch the line variable.

I'll add that unicode consoles should be one of the "new" things in .NET 4.5

And you can try to read this page: c# unicode string output

这篇关于如何用C#从.txt文件中读取西里尔字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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