-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathProgram.cs
130 lines (110 loc) · 4.08 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// <copyright file="Program.cs" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Setup.Configuration;
/// <summary>
/// The main program class.
/// </summary>
internal class Program
{
private const int REGDB_E_CLASSNOTREG = unchecked((int)0x80040154);
/// <summary>
/// Program entry point.
/// </summary>
/// <param name="args">Command line arguments passed to the program.</param>
/// <returns>The process exit code.</returns>
internal static int Main(string[] args)
{
try
{
var query = new SetupConfiguration();
var query2 = (ISetupConfiguration2)query;
var e = query2.EnumAllInstances();
var helper = (ISetupHelper)query;
int fetched;
var instances = new ISetupInstance[1];
do
{
e.Next(1, instances, out fetched);
if (fetched > 0)
{
PrintInstance(instances[0], helper);
}
}
while (fetched > 0);
return 0;
}
catch (COMException ex) when (ex.HResult == REGDB_E_CLASSNOTREG)
{
Console.WriteLine("The query API is not registered. Assuming no instances are installed.");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error 0x{ex.HResult:x8}: {ex.Message}");
return ex.HResult;
}
}
private static void PrintInstance(ISetupInstance instance, ISetupHelper helper)
{
var instance2 = (ISetupInstance2)instance;
var state = instance2.GetState();
Console.WriteLine($"InstanceId: {instance2.GetInstanceId()} ({(state == InstanceState.Complete ? "Complete" : "Incomplete")})");
var installationVersion = instance.GetInstallationVersion();
var version = helper.ParseVersion(installationVersion);
Console.WriteLine($"InstallationVersion: {installationVersion} ({version})");
if ((state & InstanceState.Local) == InstanceState.Local)
{
Console.WriteLine($"InstallationPath: {instance2.GetInstallationPath()}");
}
var catalog = instance as ISetupInstanceCatalog;
if (catalog != null)
{
Console.WriteLine($"IsPrerelease: {catalog.IsPrerelease()}");
}
if ((state & InstanceState.Registered) == InstanceState.Registered)
{
Console.WriteLine($"Product: {instance2.GetProduct().GetId()}");
Console.WriteLine("Workloads:");
PrintWorkloads(instance2.GetPackages());
}
var properties = instance2.GetProperties();
if (properties != null)
{
Console.WriteLine("Custom properties:");
PrintProperties(properties);
}
properties = catalog?.GetCatalogInfo();
if (properties != null)
{
Console.WriteLine("Catalog properties:");
PrintProperties(properties);
}
Console.WriteLine();
}
private static void PrintProperties(ISetupPropertyStore store)
{
var properties = from name in store.GetNames()
orderby name
select new { Name = name, Value = store.GetValue(name) };
foreach (var prop in properties)
{
Console.WriteLine($" {prop.Name}: {prop.Value}");
}
}
private static void PrintWorkloads(ISetupPackageReference[] packages)
{
var workloads = from package in packages
where string.Equals(package.GetType(), "Workload", StringComparison.OrdinalIgnoreCase)
orderby package.GetId()
select package;
foreach (var workload in workloads)
{
Console.WriteLine($" {workload.GetId()}");
}
}
}