Я пытаюсь зафиксировать результат команды vswhere в JSON. Когда я запускаю команду vswhere -legacy -format json прямо в командной строке, я получаю следующий отформатированный JSON:
[
{
"instanceId":"828a2470",
"installDate":"2020-11-12T15:38:31Z",
"installationName":"VisualStudio/16.8.3+30804.86",
"installationPath":"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise",
"installationVersion":"16.8.30804.86",
"productId":"Microsoft.VisualStudio.Product.Enterprise",
"productPath":"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\devenv.exe",
"state":4294967295,
"isComplete":true,
"isLaunchable":true,
"isPrerelease":false,
...
}
]
Когда я пытаюсь зафиксировать эти данные в своем приложении С#, я получаю следующий простой текст:
Visual Studio Locator version 2.8.4+ff0de50053 [query version 2.7.3111.17308]
Copyright (C) Microsoft Corporation. All rights reserved.
instanceId: 828a2470
installDate: 11/12/2020 10:38:31 AM
installationName: VisualStudio/16.8.3+30804.86
installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise
installationVersion: 16.8.30804.86
productId: Microsoft.VisualStudio.Product.Enterprise
productPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe
state: 4294967295
isComplete: 1
isLaunchable: 1
isPrerelease: 0
...
Это недопустимый JSON, поэтому я не могу его проанализировать. Как я могу получить JSON в своем коде так, как я вижу его в консоли?
Вот код:
var cmd = new Process();
cmd.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"vswhere.exe");
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
cmd.StandardInput.WriteLine("vswhere -legacy -format json");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
var result = cmd.StandardOutput.ReadToEnd();





Если вы просто запустите следующую программу, не записывая в стандартный ввод и используя аргументы, вы можете получить желаемый результат.
var cmd = new Process();
cmd.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe";
cmd.StartInfo.Arguments = "-legacy -format json";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();
cmd.WaitForExit();
string result = cmd.StandardOutput.ReadToEnd();
cmd.Dispose();
// result holds the json that can be deserialized.
// One way to deserialize is this.. but you can create a POCO if you'd be doing this alot.
dynamic obj = JsonConvert.DeserializeObject<List<ExpandoObject>>(result);
Console.WriteLine(obj[0].instanceId);
// prints
828a2470
Примечание: вы получаете простой текст, потому что вы выполняете фактический исполняемый файл без каких-либо аргументов. Вы должны использовать StartInfo.Arguments для передачи аргументов исполняемому файлу... не StandardInput.WriteLine.
Спасибо! Я не мог понять, почему JSON не перехватывается... на самом деле стоит много часов непонимания.