Creating and deleting event logs with C# .NET

The Windows event viewer contains a lot of useful information on what happens on the system:

Windows event viewer

Windows will by default write a lot of information here at differing levels: information, warning, failure, success and error. You can also write to the event log, create new logs and delete them if the code has the EventLogPermission permission. However, bear in mind that it’s quite resource intensive to write to the event logs. So don’t use it for general logging purposes to record what’s happening in your application. Use it to record major but infrequent events like shutdown, severe failure, start-up or any out-of-the-ordinary cases.

Read more of this post

Listing all performance counters on Windows with C# .NET

Performance counters in Windows can help you with finding bottlenecks in your application. There’s a long range of built-in performance counters in Windows which you can view in the Performance Monitor window:

Performance Monitor window

Right-click anywhere on the larger screen to the right and select Add Counters to add your counters to the graph. The Add Counters window will show the categories first. You can then open a category and select one or more specific counters within that category. The graph will show the real-time data immediately:

Read more of this post

4 ways to enumerate processes on Windows with C# .NET

The Process object in the System.Diagnostics namespace refers to an operating-system process. This object is the entry point into enumerating the processes currently running on the OS.

This is how you can find the currently active process:

Process current = Process.GetCurrentProcess();
Console.WriteLine(current);

…which will yield the name of the process running this short test code.

Read more of this post

How to find various machine-level system information with C# .NET

The Environment class holds a range of properties that help you describe the system your app is running on. Here come some examples with inline comments:

//returns true on my PC as it is a 64-bit OS
bool is64BitOperatingSystem = Environment.Is64BitOperatingSystem;
//returns the machine name, in my case ANDRAS1
string machineName = Environment.MachineName;
			
//returns information about the operating system version, build, major, minor etc.
OperatingSystem os = Environment.OSVersion;
//returns the platform id as an enumeration, in my case it's Win32NT
PlatformID platform = os.Platform;			
//the currently installed service pack, Service Pack 1 in my case
string servicePack = os.ServicePack;
//the toString version of the OS, this is "Microsoft Windows NT 6.1.7601 Service Pack 1" on this PC
string version = os.VersionString;

//I have 4 processors on this PC
int processorCount = Environment.ProcessorCount;

//returns 2 logical drives: C: and D:
string[] logicalDrives = Environment.GetLogicalDrives();

//this is how to find all environmental variables of the system and iterate through them
IDictionary envVars = Environment.GetEnvironmentVariables();
foreach (string key in envVars.Keys)
{
	//e.g. the JAVA_HOME env.var is set to "C:\Progra~1\Java\jdk1.7.0_51\"
	Debug.WriteLine(string.Concat("key: ", key, ": ", envVars[key]));
}

//retrieve the current CLR version, in my case it's "4.0.30319.18444"
Version clrVersion = Environment.Version;

View all posts related to diagnostics here.

4 ways to enumerate processes on Windows with C# .NET

A Process object in the System.Diagnostics namespace refers to an operating-system process. This object is the entry point into enumerating the processes currently running on the OS.

This is how you can find the currently active process:

Process current = Process.GetCurrentProcess();
Console.WriteLine(current);

…which will yield the name of the process running this short test code.

It’s probably very rare that you’ll use the above method for anything as it’s not too useful.

Read more of this post

How to find directory level information with C# .NET

In this post we saw how to extract system-level information using the Environment class in .NET. Another group of methods and properties of the Environment class lets you easily extract directory level information. Here come a couple of examples.

Here’s how to find the directory that the application is currently running in:

string currentDirectory = Environment.CurrentDirectory;

If you call the above method from a .NET app in Visual Studio then this will point to the deploy folder configured for the project, e.g. “C:\TestProjects\VariousCSharpLanguageConstructs\Various\Various\bin\Debug”

The GetFolderPath method accepts an Environment.SpecialFolder enumeration. Using this method will help you easily get a full file path reference to some well-known Windows folders like the MyDocuments folder:

string myDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string startMenuFolder = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string userProfileFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

It’s worth exploring the SpecialFolder enumeration and see what’s available there using IntelliSense.

Finally here’s how to find the location of the System directory:

string systemDirectory = Environment.SystemDirectory;

In my case it’s c:\Windows\system32

View all posts related to diagnostics here.

How to find various machine-level system information with C# .NET

The Environment class holds a range of properties that help you describe the system your app is running on. Here come some examples with inline comments:

//returns true on my PC as it is a 64-bit OS
bool is64BitOperatingSystem = Environment.Is64BitOperatingSystem;
//returns the machine name, in my case ANDRAS1
string machineName = Environment.MachineName;
			
//returns information about the operating system version, build, major, minor etc.
OperatingSystem os = Environment.OSVersion;
//returns the platform id as an enumeration, in my case it's Win32NT
PlatformID platform = os.Platform;			
//the currently installed service pack, Service Pack 1 in my case
string servicePack = os.ServicePack;
//the toString version of the OS, this is "Microsoft Windows NT 6.1.7601 Service Pack 1" on this PC
string version = os.VersionString;

//I have 4 processors on this PC
int processorCount = Environment.ProcessorCount;

//returns 2 logical drives: C: and D:
string[] logicalDrives = Environment.GetLogicalDrives();

//this is how to find all environmental variables of the system and iterate through them
IDictionary envVars = Environment.GetEnvironmentVariables();
foreach (string key in envVars.Keys)
{
	//e.g. the JAVA_HOME env.var is set to "C:\Progra~1\Java\jdk1.7.0_51\"
	Debug.WriteLine(string.Concat("key: ", key, ": ", envVars[key]));
}

//retrieve the current CLR version, in my case it's "4.0.30319.18444"
Version clrVersion = Environment.Version;

View all posts related to diagnostics here.

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: