5

I get very strange behaviour of string.Format. I form message like this:

protected override string GetMessageText(ManualEventFact reason)
        {
            var messageText = string.Format("Диспетчер закрыл событие {0}(\"{1}\")",reason.EventTemplate.DisplayName, reason.Text);
            return messageText;
        }

The letters in beginning are in Russian. But then, in calling method, i get this string: Äèñïåò÷åð çàêðûë ñîáûòèå Тревога("Тревога на объекте с точки зрения диспетчера"). This seems like string.Format returned non-unicode characters for the hard-coded words. How can i deal with this problem? P.S. I also faced this in another parts of my app.

Alex Voskresenskiy
  • 2,143
  • 2
  • 20
  • 29

1 Answers1

7

Probably the problem in in the encoding of the source file... If you are using Visual Studio, open the cs file, then go to File->Save (your cs) As, then near the Save Button, click on the small arrow, Save With Encoding, and for the Encoding select Unicode (UTF-8 with signature) - Codepage 65001.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Oh, this worked! Thanks a lot, but is there any way to force VS to save all cs files with UTF encoding? I don't want to go through all files and check if they are saved correctly... – Alex Voskresenskiy Jul 24 '15 at 10:35
  • @AlexVoskresenskiy If you create a cs file inside the Visual Studio, it should be UTF-8 by default... This clearly excludes files you wrote with another text-editor and then "included" in your project. If you have a bunch of files and you aren't sure of their encoding, I don't think there is a way *in Visual Studio* to massively change their encodings... – xanatos Jul 24 '15 at 10:37
  • @AlexVoskresenskiy There was [this question](http://stackoverflow.com/q/279673/613130), but I would be very aware of what could happen... If a file isn't encoded in UTF8/Unicode, its encoding isn't "clear" or "implicit", so conversion could "lose" data. – xanatos Jul 24 '15 at 10:38
  • Hm, anyway, it's worst a try. Thanks again for your help! – Alex Voskresenskiy Jul 24 '15 at 10:40
  • @AlexVoskresenskiy First backup your sorce files! :-) – xanatos Jul 24 '15 at 10:40