windows cmd管道不unicode甚至用/ U开关 [英] windows cmd pipe not unicode even with /U switch

查看:528
本文介绍了windows cmd管道不unicode甚至用/ U开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c#控制台程序,使用Console.WriteLine输出一些文本。然后我将这个输出管道到一个文本文件,如:

I have a little c# console program that outputs some text using Console.WriteLine. I then pipe this output into a textfile like:

c:myprogram> textfile.txt

然而,文件总是一个ansi文本文件,即使我用/ u开关启动cmd。
cmd /?关于/ u开关的说明:

However, the file is always an ansi text file, even when I start cmd with the /u switch. cmd /? says about the /u switch:


/ U导致管道或文件的内部
命令的输出为Unicode

/U Causes the output of internal commands to a pipe or file to be Unicode

当我做一个

c:echofoo> text.txt

text.txt是unicode(无BOM)

the text.txt is unicode (without BOM)

我不知道为什么管道输出的我的控制台程序到一个新的文件不创建一个unicode文件同样,我怎么可能改变呢?

I wonder why piping the output of my console program into a new file does not create an unicode file likewise and how i could change that?

我只是使用Windows Power Shell(生成一个包含正确BOM的unicode文件),但我仍然想知道如何使用cmd。

I just use Windows Power Shell (which produces a unicode file with correct BOM), but I'd still like to know how to do it with cmd.

谢谢!

推荐答案

说,会影响内部命令是否生成Unicode输出。您的程序不是cmd.exe的内部命令之一,因此/ U选项不会影响它。

The /U switch, as the documentation says, affects whether internal commands generate Unicode output. Your program is not one of cmd.exe's internal commands, so the /U option does not affect it.

要创建Unicode文本文件,您需要确保程序正在生成Unicode文本。

To create a Unicode text file, you need to make sure your program is generating Unicode text.

虽然这还不够。我遇到了这篇来自Junfeng Zhang的博客在控制台程序中编写Unicode文本。它检查标准输出句柄的文件类型。对于字符文件(控制台或LPT端口),它调用WriteFileW。对于所有其他类型的句柄(包括磁盘文件和管道),它将输出字符串转换为控制台的当前代码页。恐怕我不知道如何翻译成.Net术语。

Even that may not be enough, though. I came across this blog from Junfeng Zhang describing how to write Unicode text in a console program. It checks the file type of the standard output handle. For character files (a console or LPT port), it calls WriteFileW. For all other types of handles (including disk files and pipes), it converts the output string to the console's current code page. I'm afraid I don't know how that translates into .Net terms, though.

这篇关于windows cmd管道不unicode甚至用/ U开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆