I am having am having or have run into a very strange thing.
I wonder if others have and why it's happening.
Having run a one line program with this line System.Console.WriteLine(System.Console.OutputEncoding.EncodingName);
I see the Encoding is Western European (DOS)
Fine
Here is a list of some codepages
1200 Unicode
and 65001 utf-8
and Windows-1252 Western European (Windows)
and 850 Western European DOS
from https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx
Say I write a C sharp program to change the encoding to utf-8
class sdf
{
static void Main(string[] args)
{
System.Console.WriteLine(System.Console.OutputEncoding.EncodingName);
System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(65001);
System.Console.WriteLine(System.Console.OutputEncoding.EncodingName);
}
}
It works, it prints
Western European (DOS)
Unicode (UTF-8)
Now when I run csc again, csc crashes.
I checked my RAM for 14 hours, 8 passes, with memtest. I ran chkdsk my hard drive, all fine. And this is definitely not those, this is a coding issue. I know that because if I open up a new cmd prompt, then run csc, it doesn't crash.
So running that c sharp program, changes the shell such that the next time just running csc crashes csc itself, in that big way.
If I compile the code below, then run it, then run csc, then run csc, or csc whatever.cs, I get csc crashing.
So close the cmd prompt, Open a new one.
This time, experiment with comment and uncommenting the second line of the program
I find that if the second line (the line that changes the codepage to 850 (DOS Western Europe), is there, then it it won't crash the next time I run csc.
Whereas if I comment out that second line, so the program exits having the codepage/encoding changed to UTF-8 then then next time csc runs, csc crashes.
// uncomment the last line, and then // this runs but makes csc crash next time.
class asdf
{
static void Main()
{
System.Console.OutputEncoding = System.Text.Encoding.UTF8; //output and to utf8
System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(850);
}
}
I am not the only person that has run into something like this
though no explanation was found there https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e5f477e-0c32-4e88-acf7-d53d43d5b566/c-command-line-compiler-cscexe-immediately-crashes-when-run-in-code-page-65001-utf8?forum=csharpgeneral
I can deal with it by making sure the last line sets the codepage to 850. Though as i'll explain that's an inadequate solution..
Also i'd like to know if this is some problem with CSC that others have too. Or any other solutions.
added
uuu1.cs
// uuu1.cs
class asdf
{
static void Main()
{
System.Console.InputEncoding = System.Text.Encoding.UTF8;
System.Console.OutputEncoding = System.Text.Encoding.UTF8;
// not unicode. UTF8 means redirection will then work
System.Console.WriteLine("ჵ");
// try redirecting too..
// and try checking for csc crash or not
//System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(850);
//System.Console.InputEncoding =System.Text.Encoding.GetEncoding(850);
//problem is that when that is commented, it breaks the redirection
}
}
Adding the line / uncomment the last lines so I have
System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(850);
would stop the crash but is an inadequate solution, because for example.. If I want to redirect the output of a program to a file, then I need UTF8 all the way from beginning to end, otherwise it doesn't work
this works with the codepage 850 line uncommented
c:\blah>uuu1>r.r<ENTER>
c:\blah>type r.r <ENTER>
c:\blah>ჵ
If I uncomment the last lines, thus changing the codepage to 850 then sure csc won't crash on the next run, but the redirection doesn't work and r.r doesn't contain that character.
Added 2
Han's answer makes me notice another way of triggering this error
C:\Users\harvey\somecs3>csc<ENTER>
Microsoft (R) Visual C# Compiler version 4.0.30319.18408
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
warning CS2008: No source files specified
error CS1562: Outputs without source must have the /out option specified
C:\Users\harvey\somecs3>chcp 65001<ENTER>
Active code page: 65001
C:\Users\harvey\somecs3>csc<ENTER> <-- CRASH
C:\Users\harvey\somecs3>