Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String.Format() returns bad characters

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.

like image 354
Alex Voskresenskiy Avatar asked Jul 24 '15 10:07

Alex Voskresenskiy


Video Answer


1 Answers

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.

like image 51
xanatos Avatar answered Sep 30 '22 14:09

xanatos