Windows cmd not unicode even with / U switch

I have a small C # console program that outputs some text using Console.WriteLine. Then I pass this output to a text file, for example:

c:myprogram > textfile.txt

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

/ U Invokes the output of internal commands for a channel or file for Unicode

And it really matters when I do

c:echo "foo" > text.txt

text.txt is unicode (no specification)

I wonder why the output of my console program line to a new file does not create a unicode file in the same way and how can I change this?

I just use Windows Power Shell (which creates the unicode file with the correct specification), but I still would like to know how to do this with cmd.

Thanks!

+6
cmd text pipe unicode
source share
2 answers

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

To create a text file in Unicode, you must ensure that your program generates Unicode text.

However, this may not be enough. 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 descriptor. For character files (console or LPT port), it calls WriteFileW. For all other types of pens (including disc files and channels), it converts the output string to the current console code page. I'm afraid I don't know how this translates to .Net. Nevertheless.

+6
source share

I looked at how mscorlib implements Console.WriteLine, and it looks like he decided which encoding of the text output to use based on the call to GetConsoleOutPutCP . Thus, I am assuming (but not yet confimed) that the returned code page is distinctive for the PS console than for the cmd console, so my program really displays ansi when it starts from cmd.

+2
source share

All Articles