1
4

1 回答 1

3

The fact that these two lines:

reader.GetValue(i).ToString()                  -> "€"
reader.GetValue(i).ToString().ToCharArray()[0] -> 8364 '€'

do what you want tells me we can stop looking at data-access and MS Access, 'cos that is all working fine. The problem is simply: writing that to a file. The trick, then, is to be explicit when you create the StreamWriter. If you look at the StreamWriter constructors, you'll see that some take an Encoding. If you leave it blank, it will default to UTF-8. So: don't leave it blank. Explicitly pass in your chosen Encoding. I would recommend you figure out exactly which code-page you mean, and use:

const int CodePage = ....; // TODO: only you know this
var enc = Encoding.GetEncoding(CodePage);
using(var file = File.Create(path))
using(var writer = new StreamWriter(file, enc)) {
   ... // write the contents
}

You could also use Encoding.Default (the system's default ANSI code-page), but that is a bit hit and miss.

于 2012-05-28T19:55:47.267 回答