Get newline in a random encoding
I am sending some email to people in various cultures. The data comes from a file in the appropriate encoding. However, I need to read the 开发者_如何学编程first few lines to process them (get the subject etc.) So I do:
var lines = File.ReadAllLines(filename, encoding);
... read first few lines up to blank...
lines = String.Join(newline, lines.skip(lineNum));
However, I don't know what I should do to get the appropriate value of newline. It is different for each encoding, and I can't use Environment.NewLine because I need the newline for the specific encoding of the email recipient, not the encoding of the web server.
You're still dealing with text, so you don't need to worry about encodings (conversions to/from binary representations of character data). What you do need to potentially worry about is different representations of "new line" in terms of characters.
It's not clear what you're using to send the mail eventually - I expect whatever you use may well sort everything out for you anyway. However, RFC 822 defines lines as being separated with CRLF ("\r\n") so I'd use that.
Of course if you're also sending an HTML version of the text, that'll contain HTML tags for line/paragraph separation anyway.
You may need to read it line by line so that you don't care much about the encoding issue:
while ((line = File.ReadLine()) != null)
{
if (line != String.Empty)
lines += line + Environment.NewLine;
}
精彩评论