Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//<snippet27>
using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class PerfCounterCatCreateExistMod
{
Expand Down Expand Up @@ -44,7 +43,7 @@ public static void Main(string[] args)
//<Snippet11>
instanceName = args[2];
//<Snippet12>
machineName = args[3]=="."? "": args[3];
machineName = args[3] == "." ? "" : args[3];
//</Snippet12>
//</Snippet11>
//<Snippet13>
Expand All @@ -54,13 +53,13 @@ public static void Main(string[] args)
//<Snippet15>
//<Snippet16>
}
catch(Exception ex)
catch (Exception)
{
// Ignore the exception from non-supplied arguments.
}

// Verify that the category name is not blank.
if (categoryName.Length==0)
if (categoryName.Length == 0)
{
Console.WriteLine("Category name cannot be blank.");
return;
Expand All @@ -69,8 +68,8 @@ public static void Main(string[] args)
//</Snippet13>
// Check whether the specified category exists.
//</Snippet16>
if (machineName.Length==0)
//<Snippet17>
if (machineName.Length == 0)
//<Snippet17>
{
objectExists = PerformanceCounterCategory.Exists(categoryName);

Expand All @@ -84,33 +83,33 @@ public static void Main(string[] args)
{
objectExists = PerformanceCounterCategory.Exists(categoryName, machineName);
}
catch(Exception ex)
catch (Exception ex)
{
Console.WriteLine("Error checking for existence of " +
"category \"{0}\" on computer \"{1}\":"+"\n" +ex.Message, categoryName, machineName);
"category \"{0}\" on computer \"{1}\":" + "\n" + ex.Message, categoryName, machineName);
return;
}
}

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

// If no counter name is given, the program cannot continue.
if (counterName.Length==0)
if (counterName.Length == 0)
{
return;
}

// A category can only be created on the local computer.
//<Snippet18>
if (!objectExists)
//</Snippet18>
//</Snippet18>
{
if (machineName.Length>0)
//<Snippet19>
if (machineName.Length > 0)
//<Snippet19>
{
return;
//</Snippet19>
Expand All @@ -126,7 +125,7 @@ public static void Main(string[] args)
{
//</Snippet21>
// Check whether the specified counter exists.
if (machineName.Length==0)
if (machineName.Length == 0)
{
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
}
Expand All @@ -136,18 +135,18 @@ public static void Main(string[] args)
}

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

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

// If this is a remote computer,
// exit because the category cannot be created.
// If this is a remote computer,
// exit because the category cannot be created.
{
if (machineName.Length>0)
if (machineName.Length > 0)
{
return;
}
Expand All @@ -159,8 +158,8 @@ public static void Main(string[] args)
string userReply = Console.ReadLine();

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

// Create the category if it was deleted or it never existed.
if (createCategory)
//<Snippet23>
//<Snippet23>
{
pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp);

Console.WriteLine("Category \"{0}\" with counter \"{1}\" created.", pcc.CategoryName, counterName);
//</Snippet23>
}
else if(instanceName.Length>0)
else if (instanceName.Length > 0)
{
if (machineName.Length==0)
if (machineName.Length == 0)
{
objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
}
Expand All @@ -195,8 +194,8 @@ public static void Main(string[] args)
}

// Tell the user whether the instance exists.
Console.WriteLine("Instance \"{0}\" "+(objectExists? "exists": "does not exist")+
" in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
Console.WriteLine("Instance \"{0}\" " + (objectExists ? "exists" : "does not exist") +
" in category \"{1}\" on " + (machineName.Length > 0 ? "computer \"{2}\"." : "this computer."),
instanceName, categoryName, machineName);
//<Snippet25>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.0" />
</ItemGroup>

</Project>
</Project>
Loading
Loading