从C#中的多种编码读取文件 [英] Read file from multiple encoding in c#
问题描述
ENV:C#,VStudio 2013、4.5 Framework,Winforms,nHapi 2.3 dll
我真的需要帮助.我尝试了太多事情,并与我最好的朋友google ;-)做了大量研究.但没有运气.
我正在构建HL7发送器工具,并且正在从文件夹中读取文件.我的文件来自多个来源,我发现它们具有不同的编码:我用notepadd ++打开了它们,它们可以是没有BOM的ainsi,utf8和utf8.我的文件还包含特殊字符,例如é,à,ç,ô,....,但是当我用以下行读取文件时,它们总是放奇怪的字符:var hl7message = File.ReadAllText(e.Node.Name); >
唯一没有问题的是,源文件是在BOM中用UTF8编码的.
有没有一种方法,无论源文件的编码是什么,我都可以始终读取字符串中的文件并正确显示特殊的角色.
这是我代码的主要部分:
var hl7message = File.ReadAllText(FileName);var llphl7message = Convert.ToChar(11).ToString()+ newmessage + Convert.ToChar(28).ToString()+ Convert.ToChar(13).ToString();//获取我们必须发送的消息的大小.Byte [] bytesSent = Encoding.Default.GetBytes(llphl7message);Byte [] bytesReceived =新的Byte [256];//使用指定的服务器和端口创建套接字连接.套接字s = ConnectSocket(txtParamServer.Text,Convert.ToInt32(txtParamPort.Text));//如果套接字无法获得连接,则返回false.如果(s == null){txtLog.Text = txtLog.Text +"[" + DateTime.Now +] [错误]服务员" + txtParamServer.Text +端口" + txtParamPort.Text +"est non disponible" +"\ r \ n";返回false;}//将消息发送到服务器.s.Send(bytesSent,bytesSent.Length,0);
谢谢您的帮助对不起,英语不好:不是我的主要语言
理查德
尝试StreamReader类.它具有用于"detectEncodingFromByteOrderMarks"的参数.
字符串结果;使用(System.IO.StreamReader reader = new System.IO.StreamReader("FILENAME",true)){结果= reader.ReadToEnd();}
ENV: C#, VStudio 2013, 4.5 Framework, Winforms, nHapi 2.3 dll
I really need help on this. I have tried soo many things and did alot of research with my best friend google ;-). But no luck.
I'm building a HL7 sender tools and I'm reading files from a folder. My files come from multiple sources and I found that they have different encoding : I have open them with notepadd++ and they can be ainsi, utf8 et utf8 witout BOM. My files also contains special caracters like é,à,ç,ô, .... but they always put weird caracter when I read the file with this line : var hl7message = File.ReadAllText(e.Node.Name);
The only time I don't have any problem is when the source file is encoded in UTF8 with the BOM.
Is there a way that no matter what the source file encoding is, I can alway read the file in a string and have the special caracter show correctly.
This is the main portion of my code :
var hl7message = File.ReadAllText(FileName);
var llphl7message = Convert.ToChar(11).ToString() + newmessage + Convert.ToChar(28).ToString() + Convert.ToChar(13).ToString();
// Get the size of the message that we have to send.
Byte[] bytesSent = Encoding.Default.GetBytes(llphl7message);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(txtParamServer.Text, Convert.ToInt32(txtParamPort.Text));
// If the socket could not get a connection, then return false.
if (s == null)
{
txtLog.Text = txtLog.Text + "[" + DateTime.Now + "] [ERR] Serveur " + txtParamServer.Text + " sur le port " + txtParamPort.Text + " est non disponible" + "\r\n";
return false;
}
// Send message to the server.
s.Send(bytesSent, bytesSent.Length, 0);
Thank you for you help Sorry for the bad english : not my main language
Richard
Try the StreamReader class. It has a parameter for "detectEncodingFromByteOrderMarks".
string result;
using (System.IO.StreamReader reader = new System.IO.StreamReader("FILENAME", true))
{
result = reader.ReadToEnd();
}
这篇关于从C#中的多种编码读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!