【发布时间】:2015-09-06 12:57:41
【问题描述】:
我正在经历或遇到一件非常奇怪的事情。
我想知道其他人是否有,为什么会这样。
用这一行 System.Console.WriteLine(System.Console.OutputEncoding.EncodingName); 运行了一个单行程序,我看到编码是 Western European (DOS)
很好
这里是一些代码页的列表
1200 Unicode 和 65001 utf-8 和 Windows-1252 Western European (Windows) 和 850 Western European DOS 来自 https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx
假设我写了一个 C 语言程序来将编码更改为 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);
}
}
它工作,它打印
Western European (DOS)
Unicode (UTF-8)
现在当我再次运行 csc 时,csc 崩溃了。
我用 memtest 检查了我的 RAM 14 小时,8 次通过。我运行 chkdsk 我的硬盘,一切都很好。这绝对不是那些,这是一个编码问题。 我知道,因为如果我打开一个新的 cmd 提示符,然后运行 csc,它不会崩溃。
所以运行那个 c sharp 程序,会改变 shell,这样下一次运行 csc 时 csc 本身就崩溃了。
如果我编译下面的代码,然后运行它,然后运行 csc,然后运行 csc 或 csc whatever.cs,我会遇到 csc 崩溃。
所以关闭cmd提示符,打开一个新的。
这一次,尝试注释和取消程序第二行的注释
我发现如果第二行(将代码页更改为 850(DOS 西欧)的那行存在,那么下次我运行 csc 时它就不会崩溃。
而如果我注释掉第二行,那么程序退出并将代码页/编码更改为 UTF-8 然后下次运行 csc 时,csc 崩溃。
// 取消最后一行的注释,然后 // 这会运行,但下次 csc 会崩溃。
class asdf
{
static void Main()
{
System.Console.OutputEncoding = System.Text.Encoding.UTF8; //output and to utf8
System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(850);
}
}
我不是唯一遇到这种情况的人
我可以通过确保最后一行将代码页设置为 850 来处理它。尽管我将解释这是一个不充分的解决方案..
我也想知道这是否是其他人也有的 CSC 问题。或任何其他解决方案。
添加
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
}
}
添加行/取消注释最后一行,所以我有
System.Console.OutputEncoding=System.Text.Encoding.GetEncoding(850);
会停止崩溃但不是一个不充分的解决方案,因为例如..如果我想将程序的输出重定向到文件,那么我需要从头到尾一直使用 UTF8,否则它不起作用
这适用于未注释的代码页 850 行
c:\blah>uuu1>r.r<ENTER>
c:\blah>type r.r <ENTER>
c:\blah>ჵ
如果我取消注释最后几行,从而将代码页更改为 850,则确保 csc 在下次运行时不会崩溃,但重定向不起作用,并且 r.r 不包含该字符。
添加了 2 个
韩的回答让我注意到触发此错误的另一种方式
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>
【问题讨论】:
-
你如何编译和运行这个?
-
@CodeCaster 将代码放在扩展名为
.cs的文件中,例如aaa1.cs然后运行 csc aaa1.cs -
@CodeCaster 开发命令提示符或常规命令提示符有同样的问题。顺便说一句,我也设置了字体关于如何更改字体的步骤,描述了techrepublic.com/blog/windows-and-office/…
-
@CodeCaster 将字体添加到命令提示符的过程本身就是一篇很长的文章。该帖子的大小会增加一倍,因此我链接到了一篇关于这样做的文章。无论如何,许多熟悉此问题的人都会在命令提示符中添加 unicode 字体。这里的问题在于 csc.exe 而不是将字体添加到命令提示符。可以在不向命令提示符添加字体的情况下执行此操作,只是不太清楚。
标签: c# windows command-line csc