-
Notifications
You must be signed in to change notification settings - Fork 99
Encoding
vswhere will print localized text in Unicode to a console, but like our native compilers when printing text redirected to a file will use the console code page by default. Even if Windows is localized, the OEM code page of the console might still be 437, which is equivalent to Windows code page 1252 consisting, primarily, of Western European characters.
You can work around this by passing -utf8
to vswhere 2.5 or newer, or changing the console code page and output encoding, depending on your console environment.
Using Node.js as an example, you can force UTF8 output regardless of the system code page.
const cp = require('child_process');
cp.execFile('vswhere.exe', ['-latest', '-format', 'json', '-utf8'], (err, stdout, stderr) => {
var instances = JSON.parse(stdout);
if (instances.length > 0) {
console.log(instances[0].description);
}
});
Note that in this example, since you're printing to the console, the active code page must still be compatible with the expected output encoding or text may not render correctly. The active code page should not matter, however, if you're not printing any localized text to the console.
With cmd, you can simply run chcp <code page>
to change the default code page used to convert Unicode text when redirecting to a file. See MSDN. chcp
accepts ANSI code pages as well as OEM code pages.
C:\> vswhere -property description -latest
Бесплатная полнофункциональная интегрированная среда разработки для учащихся, разработчиков решений с открытым кодом и индивидуальных разработчиков
C:\> chcp
Active code page: 437
C:\> chcp 1251
Active code page: 1251
C:\> vswhere -property description -latest > %TEMP%\description.txt
Without changing the code page - unless your fallback system code page was already set to 1251 (OEM code page 866) - %TEMP%\description.txt would contain garbled text.
Note that even when passing -utf8
text still may not render correctly in the console host unless you run chcp <code page>
to a code page compatible with the expected output encoding; however, if redirecting to a file or piping to another command UTF8 encoding will be used.
With PowerShell the process is much the same, except that you also need to set the [Console]::OutputEncoding
to either Unicode, UTF8, or the same ANSI code page.
PS C:\> vswhere -property description -latest
Бесплатная полнофункциональная интегрированная среда разработки для учащихся, разработчиков решений с открытым кодом и индивидуальных разработчиков
PS C:\> chcp
Active code page: 437
PS C:\> [Console]::OutputEncoding.WindowsCodePage
1252
PS C:\> chcp 1251
Active code page: 1251
PS C:\> [Console]::OutputEncoding = [Text.Encoding]::GetEncoding(1251)
PS C:\> vswhere -property description -latest > $env:TEMP\description.txt
PS C:\> $description = vswhere -property description -latest
As with cmd, %TEMP%\description.txt will now contain properly localized text as well as the $description
variable.
Note that PowerShell will always output Unicode text when redirected to a file. The [Console]::OutputEncoding
will help transcode text, but the resulting file will always be Unicode. It is for this reason that, for example, the XML format does not emit an encoding
preprocessor attribute: it would be incorrect to specify encoding="utf-8"
when the file itself is Unicode.
Copyright (C) Microsoft Corporation. All rights reserved. Licensed under the MIT license. See LICENSE.txt in the project root for license information.