Categories

Get CPU type and speed from registry

This snippet shows how to get the current CPU and speed from the registry

 

using System;
using Microsoft.Win32;

namespace Examples
{
 class Program
 {

 static void Main(string[] args)
 {
 RegistryKey RegKey = Registry.LocalMachine;
 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
 Object cpuSpeed = RegKey.GetValue("~MHz");
 Object cpuType = RegKey.GetValue("VendorIdentifier");
 Console.WriteLine("You have a {0} running at {1} MHz.", cpuType, cpuSpeed);
 Console.ReadLine();
 }

 }
}

Get installed applications in a system using the registry


using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System;
using Microsoft.Win32;

namespace Examples
{
 class Program
 {

 static void Main(string[] args)
 {

 string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
 using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
 {
 foreach (string subkey_name in key.GetSubKeyNames())
 {
 using (RegistryKey subkey = key.OpenSubKey(subkey_name))
 {
 Console.WriteLine(subkey.GetValue("DisplayName"));
 }
 }
 Console.ReadLine();
 }
 }

 }
}

How to get thread id

This example shows how to get the thread id



using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System;


namespace Examples
{
 class Program
 {
 [DllImport("kernel32.dll")]
 public static extern uint GetCurrentThreadId();

 static void Main(string[] args)
 {

 uint threadId = GetCurrentThreadId();
 int managedThreadId = Thread.CurrentThread.ManagedThreadId;

 Console.WriteLine("Thread Id: {0}", threadId);
 Console.WriteLine("Managed Thread Id: {0}", managedThreadId);
 Console.ReadLine();
 }

 }
}

View the contents of a zip file using SharpCompress

View the contents of a zip file using SharpCompress. You can get this from http://sharpcompress.codeplex.com

Example

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SharpCompress.Reader;
using SharpCompress.Common;

namespace ZipExample
{
class Program
{
static void Main(string[] args)
{
//change the path to a zip file on your PC
using (Stream stream = File.OpenRead(@”C:\Users\iain\Downloads\wexample.zip”))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Console.WriteLine(reader.Entry.FilePath);
reader.WriteEntryToDirectory(@”C:\temp”, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
}
}
}
}

Write to the event log

This example will write an entry to the application event log

using System;
using System.Diagnostics;

namespace Test
{
class Program
{
static void Main(string[] args)
{
EventLog.WriteEntry(“Service example”, “Service example”, System.Diagnostics.EventLogEntryType.Information);
}
}
}

List all installed Windows services

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;

namespace Test
{
class Program
{
static void Main(string[] args)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
Console.WriteLine(“Installed: ” + service.ServiceName);
}
}
}
}