如何使用C#修复从文件中读取的显示文本 [英] How can I fix the display text which is read from a file using C#
本文介绍了如何使用C#修复从文件中读取的显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有C#的WindowForm我想读取文件并显示到标签。但结果不是我的期望。它显示为LP#@ $,因为我的数据是Nghia Nam Honag。 我该如何解决? 这是我的代码:
private void btnFile1_Click( object sender,EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
string str = string .Empty;
string chuoiNoi = string .Empty;
string line = string .Empty;
if (openFile.ShowDialog()== DialogResult.OK){
str = openFile.FileName;
尝试
{
使用(StreamReader sr = new StreamReader(str))
{
while ((line = sr.ReadLine() )!= null ){
chuoiNoi = chuoiNoi + + line;
}
lbFileA.Text = chuoiNoi;
}
} catch (Exception ex){
Console.Write( 您有错误:);
Console.Write(ex.Message);
}
}
}
我尝试过:
我试图添加encoding.UTF-8但不成功
解决方案
incase我的数据是Nghia Nam Honag。我该如何解决? 这是我的代码:
私有 void btnFile1_Click( object sender,EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
string str = string .Empty;
string chuoiNoi = string .Empty;
string line = string .Empty;
if (openFile.ShowDialog()== DialogResult.OK){
str = openFile.FileName;
尝试
{
使用(StreamReader sr = new StreamReader(str))
{
while ((line = sr.ReadLine() )!= null ){
chuoiNoi = chuoiNoi + + line;
}
lbFileA.Text = chuoiNoi;
}
} catch (Exception ex){
Console.Write( 您有错误:);
Console.Write(ex.Message);
}
}
}
我尝试过:
我试图添加encoding.UTF-8但不成功
很可能是不匹配这是因为您的源是Unicode:尝试使用Encoding.Unicode选项阅读:[ ^ ]。
另外,请确保使用标签字体支持Unicode,比如'Consolas。
在这里显示的代码中,没有真正的有机原因我可以看到逐行阅读文件行,并使用添加的字符串将运行内存使用方式。
考虑一次读取整个文件。
fyi:标签是控件显示文件整个内容的奇怪选择。
您好,
来自msdn:
File.ReadAllText方法(字符串)打开文本文件,读取文件的所有行,以及然后关闭文件。 链接到这里
string filecontent = File.ReadAllText(filepath);
谢谢
I am using WindowForm with C# I want to read a file and display to a label. But the result is not my expectation. It displays like "LP#@$" incase my data is "Nghia Nam Honag". How can I fix it? Here is my code:
private void btnFile1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
string str = string.Empty;
string chuoiNoi = string.Empty;
string line = string.Empty;
if(openFile.ShowDialog() == DialogResult.OK){
str = openFile.FileName;
try
{
using (StreamReader sr = new StreamReader(str))
{
while((line = sr.ReadLine()) != null){
chuoiNoi = chuoiNoi + " " + line;
}
lbFileA.Text = chuoiNoi;
}
}catch(Exception ex){
Console.Write("You have an error: ");
Console.Write(ex.Message);
}
}
}
What I have tried:
I tried to add encoding.UTF-8 but unsuccess
解决方案
" incase my data is "Nghia Nam Honag". How can I fix it? Here is my code:
private void btnFile1_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); string str = string.Empty; string chuoiNoi = string.Empty; string line = string.Empty; if(openFile.ShowDialog() == DialogResult.OK){ str = openFile.FileName; try { using (StreamReader sr = new StreamReader(str)) { while((line = sr.ReadLine()) != null){ chuoiNoi = chuoiNoi + " " + line; } lbFileA.Text = chuoiNoi; } }catch(Exception ex){ Console.Write("You have an error: "); Console.Write(ex.Message); } } }
What I have tried:
I tried to add encoding.UTF-8 but unsuccess
Most likely the "mis-match" here is because your source is Unicode: try reading with Encoding.Unicode option: [^].
Also, make sure you are using a Font for the Label that supports Unicode, like 'Consolas.
In the code you show here, there is no real "organic" reason I can see to read the file line-by-line, and use of addition of Strings is going to run the use of memory way up.
Consider reading the whole file at one time.
fyi: a Label is a "strange" choice for a Control to display the entire contents of a file.
Hello ,
From msdn :
File.ReadAllText Method (String)Opens a text file, reads all lines of the file, and then closes the file. Link Here
string filecontent= File.ReadAllText(filepath);
Thanks
这篇关于如何使用C#修复从文件中读取的显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文