1

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

0

1 Answer 1

1

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();
            }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.