1. Write windows Event Log
Use Windows Event log to ensure that your app is working as expected. Useful in notifying the System Administrator, in case some failures or important events occur?
Sample Code:
using System.Diagnostics; public void WriteEventLog(string strCallerName, string strLogLine) { if (!System.Diagnostics.EventLog.SourceExists(strCallerName)) { System.Diagnostics.EventLog.CreateEventSource(strCallerName, "Application"); } EventLog EventLog1 = new EventLog(); EventLog1.Source = strCallerName; EventLog1.WriteEntry (strLogLine, EventLogEntryType.Warning) }
2. Single Instance of Your Application
You can prevent multiple instances of your application by using Mutex. This ensures that your application is having only one instance at a time, preventing it from any unexpected outcomes. Here is the code for the Main method:
using System.Threading;
static void Main() { bool bAppFirstInstance; oMutex = new Mutex(true, "Global\\" +demoApp, out bAppFirstInstance); if(bAppFirstInstance) Application.Run(new form demoApp() or class demoApp()); else MessageBox.Show("Only one instance of application is allowed. ", "Startup warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); }
3. Use Environment Variables
With .NET, we have a class which lets us access all system information. OS version, CLR version, user name, values of system variables such as the temp folder, physical memory mapped to your app etc. All this information can be extracted with the environment class:
using System;
public static void GetEnvironmentInfo() { // Fully path of the current directory Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory); // Gets the name of local computer Console.WriteLine("MachineName: {0}", Environment.MachineName); // Version of the OS Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString()); // Full path of the system directory Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory); // Network domain name Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName); // Whether the current process is running in user interactive mode Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive); // User who started the current thread Console.WriteLine("UserName: {0}", Environment.UserName); // Major, minor, build, and revision numbers of CLR Console.WriteLine("CLRVersion: {0}", Environment.Version.ToString()); // Amount of physical memory mapped to the process Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet); // Returns values of Environment variables Console.WriteLine("ExpandEnvironmentVariables: {0}", Environment.ExpandEnvironmentVariables("System drive: " + "%SystemDrive% System root: %SystemRoot%")); // names of the logical drives Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", Environment.GetLogicalDrives())); }
The above function will work with console applications only (as it has Console.Writelines). For your use, modify it the way you need..