lantsev1981
lantsev1981

Reputation: 63

Error string encoding (Windows 10 + Visual Studio 2015 + Net 4.6)

My code:

Keys = new Dictionary<string, string>();
Keys.Add("Набег_0", "raid_0");

When I get Keys.ElementAt(0), I have this: {[Íàáåã_0, raid_0]}. Of course, when I run the program, key = "Набег_0" is not defined and the program crashes with a System.Collections.Generic.KeyNotFoundException

This code worked fine when I had Windows 8.1 + Visual Studio 2013 + net 3.5

How do I fix this?

Upvotes: 6

Views: 6130

Answers (5)

vasek
vasek

Reputation: 2849

If you don't want to change source code encoding, you can add encoding element into .csproj file. This helped for me (added into <PropertyGroup> element):

<CodePage>1250</CodePage>

But of course just a temporary hack, useless for solutions with many projects.

Upvotes: 4

Hans Passant
Hans Passant

Reputation: 942408

You somehow convinced the C# compiler that your source code was written in code page 1251, the default system code page in Eastern Europe and Russia. That's usually caused by the text file missing the utf-8 BOM. Unclear how this happened, maybe you created the file with a text editor other than the one built into Visual Studio. Maybe it got mangled by source control, the ones with a Unix background tend to drop the BOM.

Open the source file in Visual Studio and ensure it still reads correctly. Then use File > Save As, click the arrow on the Save button, select "with encoding" and pick "Unicode (UTF-8 with signature)".

Also make sure that the default is still good. File > Advanced Save Options > change the Encoding if necessary. If you habitually use another text editor then you'll want configure it so it saves files with a BOM.

Upvotes: 7

matra
matra

Reputation: 10223

This is a known bug in Visual Studio 2015 Relase version. See https://github.com/dotnet/roslyn/issues/4022. It is already fixed and will be available in next version of toolset (1.1)

Upvotes: 4

Sergey Belikov
Sergey Belikov

Reputation: 420

I am also having this issue with a local project that worked perfectly in Win8.1/VS2013. So this is not related with TFS or any other repository. Resaving to UTF-8 helps. Also, I have a Russian localized Visual Studio that was updated to English by language pack (hate localized IDE and MSDN).

Upvotes: 0

Pavel Zhirkov
Pavel Zhirkov

Reputation: 51

I have the same issue, in my case it was ReSharper who saved my files in windows-1251 after applying "move class to separate file" refactoring.

I've used this test to convert all my cs files in repo to UTF-8.

    [Test]
    public void UpdateEncoding()
    {
        string path = @"C:\dev\Cash\src";
        foreach (var file in Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories))
        {
            if (HasBom(file))
                continue;

            Console.WriteLine(file);

            var content = File.ReadAllText(file, Encoding.GetEncoding("windows-1251"));
            File.WriteAllText(file, content, Encoding.UTF8);
        }
    }

    private bool HasBom(string file)
    {
        using (var strm = new FileStream(file, FileMode.Open))
        {
            foreach (var b in Encoding.UTF8.GetPreamble())
            {
                if (strm.ReadByte() != b)
                    return false;
            }

            return true;
        }
    }

Upvotes: 5

Related Questions