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

5

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?

Piliferous answered 1/8, 2015 at 13:10 Comment(4)
Sound like you always had the bug. Somehow the key Íàáåã_0 is at element zero instead of Ha6er_0.Inward
The keys are written in Russian. The aim is to get the English meaning. Before you upgrade to Windows 10 + .Net 4.6 + Visual Studio 2015 code worked perfectly. It is clearly seen that the new visual studio no longer understand Russian language. Can blame windows or .net. I would not want to change all of their manuals in English language, since it would require a lot of changes in the program. P. S. sorry for auto translatePiliferous
It is the encoding. Looks like you have Unicode characters. Stream class default to ascii encoding. Looks like you have to specify Unicode encoding in one of you classes.Inward
I have the same problem. When the file is not UTF-8 encoded Windows10/VisualStudio2015 assumes that file has wrong encoding. Everything looks OK until I compile application.Spaceship
G
7

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.

Gabionade answered 1/8, 2015 at 16:31 Comment(2)
Re-save the files in UTF-8 helped, but still not clear how it happened that all my files were in ANSI. Possibly to blame for TFS. Now I have to browse all the files for the presence of UTF-8 encoding, and the project... Thank you very much for your help!Piliferous
It worked perfect before Visual Studio 2015 even with files in 1251 codepage. It seems to be bug in Visual Studio as was mentioned in next answer hear.Doyledoyley
S
5

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;
        }
    }
Stung answered 12/8, 2015 at 22:34 Comment(1)
I am not sure, but clearing Resharper cache may help to solve this problem.Stringfellow
S
4

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)

Sankaran answered 21/9, 2015 at 16:13 Comment(0)
E
4

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.

Estafette answered 4/11, 2015 at 17:1 Comment(2)
This hack makes my visual studio designer die with "Could not find type" errors.Tobitobiah
I strongly recommend updating to Update 1 RC which fixes this annoying bug (Version 14.0.24627.00).Estafette
T
0

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).

Thule answered 8/9, 2015 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.