It’s a common requirement for a software developer to have to access the Windows Registry. Many applications store their settings within the registry, not to mention Windows itself. So in this article I will show you how to do this, and also how to create and delete registry keys.
Although many applications use the registry for storing their settings and configurations, nowadays it’s not the recommended practise any more. Instead you should store your application’s settings in an XML file – such as the Settings.settings file in your C# project. If you don’t know what the Settings.settings file is I wrote an article on it which you can read here – Using the Settings file in C#.
Before you start testing any code which manipulates the registry be sure you know exactly what you’re editing. Changing, or even worse, deleting registry keys and values which are used by Windows can cause it to fail to load, and you would have to try to restore Windows back or maybe even reinstall it completely. So don’t say you haven’t been warned!
On the positive side, the registry manipulation we will be looking at in this article will not affect Windows so it is quite safe if you follow my examples.
The Registry and the RegistryKey Classes
These are the classes we will be using to manipulate the registry. They are both found under the Microsoft.Win32
namespace so you will have to add it to your project to use the classes.
The Registry
class provides us with objects that represent the root keys in the Windows registry. It also contains static methods to access key and value pairs but we will not be using these in this article. The root key representations are as follows:
Registry Value |
Class Property |
HKEY_CLASSES_ROOT |
Registry.ClassesRoot |
HKEY_CURRENT_CONFIG |
Registry.CurrentConfig |
HKEY_CURRENT_USER |
Registry.CurrentUser |
KEY_DYN_DATA |
Registry.DynData |
HKEY_LOCAL_MACHINE |
Registry.LocalMachine |
HKEY_PERFORMANCE_DATA |
Registry.PerformanceData |
HKEY_USERS |
Registry.Users |
The RegistryKey
class represents a key-level node in the Windows registry and this is the class we will be making the most use of.
[continue reading…]
The Windows Event Log is a great place to log your application’s errors or major events because it is easily accessible by administrators since all Windows Event logs can be managed from the same console. This makes the administrator’s life easier because he/she does not have to monitor logs stored in multiple directories all over the place. However, if your application generates large amounts of logs, I recommend you create your own logging system instead, so as not to over clutter the Windows Event Logs.
In this short article I am going to show you how to write to the Windows Event Log. It is actually quite simple – all you have to do is use the .NET System.Diagnostics.EventLog
class.
Below is an example of how to use this class to write to the log:
static void Main(string[] args)
{
WriteEventLogEntry("This is an entry in the event log by daveoncsharp.com");
}
private static void WriteEventLogEntry(string message)
{
// Create an instance of EventLog
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
// Check if the event source exists. If not create it.
if (!System.Diagnostics.EventLog.SourceExists("TestApplication"))
{
System.Diagnostics.EventLog.CreateEventSource("TestApplication", "Application");
}
// Set the source name for writing log entries.
eventLog.Source = "TestApplication";
// Create an event ID to add to the event log
int eventID = 8;
// Write an entry to the event log.
eventLog.WriteEntry(message,
System.Diagnostics.EventLogEntryType.Error,
eventID);
// Close the Event Log
eventLog.Close();
}
[continue reading…]
In this article – part two of creating a screen saver in C#, we are going to pick up from where we left off in part one, which I suggest you read before continuing with this article if you haven’t already done so.
So far (in part one) we have created a Settings
class and a form where the user can edit the screen saver settings. The next part is creating the actual screen saver form.
The Screen Saver Form
The screen saver form has the following custom properties set:
this.BackColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "ScreenSaverForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
The form also has a System.Windows.Forms.Timer
control placed on it which will be used as the interval between drawing each shape.
Form Mouse Events
The form must also handle mouse down and mouse move events so that when the mouse is either moved or clicked the screen saver will exit. Below is the code for these events plus the declarations we will be needing for the screen saver:
// Graphics object to use for drawing
private Graphics graphics;
// Random object for randomizing drawings
private Random random;
// Settings object which contains the screensaver settings
private Settings settings;
// Flag used to for initially setting mouseMove location
private bool active;
// Used to determine if the Mouse has actually been moved
private Point mouseLocation;
// Used to indicate if screensaver is in Preview Mode
private bool previewMode = false;
private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
{
if (!this.previewMode)
{
// If the mouseLocation still points to 0,0, move it to its actual
// location and save the location. Otherwise, check to see if the user
// has moved the mouse at least 10 pixels.
if (!this.active)
{
this.mouseLocation = new Point(e.X, e.Y);
this.active = true;
}
else
{
if ((Math.Abs(e.X - this.mouseLocation.X) > 10) ||
(Math.Abs(e.Y - this.mouseLocation.Y) > 10))
{
// Exit the screensaver
Application.Exit();
}
}
}
}
private void ScreenSaverForm_MouseDown(object sender, MouseEventArgs e)
{
if (!this.previewMode)
{
// Exit the screensaver if not in preview mode
Application.Exit();
}
}
[continue reading…]
In this two-part article series I am going to show you how to create a Windows screen saver using C#.
A screen saver is not much more than a normal Windows Form with no border and some logic to display something on the form. For it to be a Windows screen saver the compiled application must end with the .scr extension and it must also accept command line parameters, since Windows sends a parameter telling the screen saver to either preview, run, or display its settings. And that’s basically it – there is really not much to creating a screen saver, but I will explain it all in detail throughout this article series anyway. But first let’s design our screen saver.
Designing the Screen Saver
Our screen saver will be displaying random shapes with random sizes and random colours and random levels of transparency (that’s a lot of randomness… :)). It will look something like this:
Do you like it? If you do, continue reading because we are about to create this very screen saver. If you are in a hurry and don’t have time to read this article (or are too lazy to read :P) you can always just download it from the link at the end of this article series.
[continue reading…]
Back in the late 90’s before the invention of the .NET Framework, there was no “easy” way to add an icon to the system tray of Windows. Developers who preferred to use Microsoft technologies were either developing with C++, Visual Basic, or maybe even FoxPro, and none of these technologies provided an easy way to integrate a notify icon into your application. This was a shame because back then the notify icon was considered as a cool feature to have for your application – every developer wanted one.
Then with the invention of the .NET Framework came the NotifyIcon
.NET Component. Finally there was an easy way to add a tray icon to your application.
In this article I am going to show you how to use the NotifyIcon
component.
To start off create a C# Windows Forms Application project and on your main form drag the NotifyIcon
component from the toolbox to the form. From the component’s properties you can set the icon to be displayed in the system tray, the text to be displayed when the mouse hovers over the tray icon, balloon tip settings, and other common properties that most of the .NET components have.
[continue reading…]