Skip to content

Commit eee6890

Browse files
authored
Modernize remaining System.Diagnostics C# snippets (#13034)
1 parent 71b9c6f commit eee6890

166 files changed

Lines changed: 1640 additions & 1477 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFramework>net10.0-windows</TargetFramework>
6-
<UseWindowsForms>true</UseWindowsForms>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
75
</PropertyGroup>
8-
96
<ItemGroup>
107
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.0" />
118
</ItemGroup>
12-
139
</Project>

snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//<snippet27>
22
using System;
33
using System.Diagnostics;
4-
using Microsoft.VisualBasic;
54

65
class PerfCounterCatCreateExistMod
76
{
@@ -44,7 +43,7 @@ public static void Main(string[] args)
4443
//<Snippet11>
4544
instanceName = args[2];
4645
//<Snippet12>
47-
machineName = args[3]=="."? "": args[3];
46+
machineName = args[3] == "." ? "" : args[3];
4847
//</Snippet12>
4948
//</Snippet11>
5049
//<Snippet13>
@@ -54,13 +53,13 @@ public static void Main(string[] args)
5453
//<Snippet15>
5554
//<Snippet16>
5655
}
57-
catch(Exception ex)
56+
catch (Exception)
5857
{
5958
// Ignore the exception from non-supplied arguments.
6059
}
6160

6261
// Verify that the category name is not blank.
63-
if (categoryName.Length==0)
62+
if (categoryName.Length == 0)
6463
{
6564
Console.WriteLine("Category name cannot be blank.");
6665
return;
@@ -69,8 +68,8 @@ public static void Main(string[] args)
6968
//</Snippet13>
7069
// Check whether the specified category exists.
7170
//</Snippet16>
72-
if (machineName.Length==0)
73-
//<Snippet17>
71+
if (machineName.Length == 0)
72+
//<Snippet17>
7473
{
7574
objectExists = PerformanceCounterCategory.Exists(categoryName);
7675

@@ -84,33 +83,33 @@ public static void Main(string[] args)
8483
{
8584
objectExists = PerformanceCounterCategory.Exists(categoryName, machineName);
8685
}
87-
catch(Exception ex)
86+
catch (Exception ex)
8887
{
8988
Console.WriteLine("Error checking for existence of " +
90-
"category \"{0}\" on computer \"{1}\":"+"\n" +ex.Message, categoryName, machineName);
89+
"category \"{0}\" on computer \"{1}\":" + "\n" + ex.Message, categoryName, machineName);
9190
return;
9291
}
9392
}
9493

9594
//</Snippet15>
9695
// Tell the user whether the specified category exists.
97-
Console.WriteLine("Category \"{0}\" "+ (objectExists? "exists on ": "does not exist on ")+
98-
(machineName.Length>0? "computer \"{1}\".": "this computer."), categoryName, machineName);
96+
Console.WriteLine("Category \"{0}\" " + (objectExists ? "exists on " : "does not exist on ") +
97+
(machineName.Length > 0 ? "computer \"{1}\"." : "this computer."), categoryName, machineName);
9998
//</Snippet14>
10099

101100
// If no counter name is given, the program cannot continue.
102-
if (counterName.Length==0)
101+
if (counterName.Length == 0)
103102
{
104103
return;
105104
}
106105

107106
// A category can only be created on the local computer.
108107
//<Snippet18>
109108
if (!objectExists)
110-
//</Snippet18>
109+
//</Snippet18>
111110
{
112-
if (machineName.Length>0)
113-
//<Snippet19>
111+
if (machineName.Length > 0)
112+
//<Snippet19>
114113
{
115114
return;
116115
//</Snippet19>
@@ -126,7 +125,7 @@ public static void Main(string[] args)
126125
{
127126
//</Snippet21>
128127
// Check whether the specified counter exists.
129-
if (machineName.Length==0)
128+
if (machineName.Length == 0)
130129
{
131130
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
132131
}
@@ -136,18 +135,18 @@ public static void Main(string[] args)
136135
}
137136

138137
// Tell the user whether the counter exists.
139-
Console.WriteLine("Counter \"{0}\" "+(objectExists? "exists": "does not exist")+
140-
" in category \"{1}\" on "+(machineName.Length>0? "computer \"{2}\".": "this computer."),
138+
Console.WriteLine("Counter \"{0}\" " + (objectExists ? "exists" : "does not exist") +
139+
" in category \"{1}\" on " + (machineName.Length > 0 ? "computer \"{2}\"." : "this computer."),
141140
counterName, categoryName, machineName);
142141
//</Snippet20>
143142

144143
// If the counter does not exist, consider creating it.
145144
if (!objectExists)
146145

147-
// If this is a remote computer,
148-
// exit because the category cannot be created.
146+
// If this is a remote computer,
147+
// exit because the category cannot be created.
149148
{
150-
if (machineName.Length>0)
149+
if (machineName.Length > 0)
151150
{
152151
return;
153152
}
@@ -159,8 +158,8 @@ public static void Main(string[] args)
159158
string userReply = Console.ReadLine();
160159

161160
// If yes, delete the category so it can be recreated later.
162-
if (userReply.Trim().ToUpper()=="Y")
163-
//<Snippet22>
161+
if (userReply.Trim().ToUpper() == "Y")
162+
//<Snippet22>
164163
{
165164
PerformanceCounterCategory.Delete(categoryName);
166165
//</Snippet22>
@@ -176,16 +175,16 @@ public static void Main(string[] args)
176175

177176
// Create the category if it was deleted or it never existed.
178177
if (createCategory)
179-
//<Snippet23>
178+
//<Snippet23>
180179
{
181180
pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp);
182181

183182
Console.WriteLine("Category \"{0}\" with counter \"{1}\" created.", pcc.CategoryName, counterName);
184183
//</Snippet23>
185184
}
186-
else if(instanceName.Length>0)
185+
else if (instanceName.Length > 0)
187186
{
188-
if (machineName.Length==0)
187+
if (machineName.Length == 0)
189188
{
190189
objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
191190
}
@@ -195,8 +194,8 @@ public static void Main(string[] args)
195194
}
196195

197196
// Tell the user whether the instance exists.
198-
Console.WriteLine("Instance \"{0}\" "+(objectExists? "exists": "does not exist")+
199-
" in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
197+
Console.WriteLine("Instance \"{0}\" " + (objectExists ? "exists" : "does not exist") +
198+
" in category \"{1}\" on " + (machineName.Length > 0 ? "computer \"{2}\"." : "this computer."),
200199
instanceName, categoryName, machineName);
201200
//<Snippet25>
202201
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<OutputType>Library</OutputType>
3+
<OutputType>Exe</OutputType>
54
<TargetFramework>net10.0</TargetFramework>
65
</PropertyGroup>
7-
86
<ItemGroup>
97
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.0" />
108
</ItemGroup>
11-
12-
</Project>
9+
</Project>

0 commit comments

Comments
 (0)