System Programing

 

Saturday, May 30, 2009

Retrieve the path to the Windows System directory

/*
Retrieve the path to the Windows System directory
-
ex. C:\Windows\System32\
*/

string winDir = Environment.GetFolderPath(Environment.SpecialFolder.System)

Display information about all of the drives on the current system

/*
The following code example demonstrates the use of the DriveInfo
class to display information about all of the drives on the current system.
-
This code produces output similar to the following:

Drive A:\
File type: Removable
Drive C:\
File type: Fixed
Volume label:
File system: FAT32
Available space to current user: 4770430976 bytes
Total available space: 4770430976 bytes
Total size of drive: 10731683840 bytes
Drive D:\
File type: Fixed
Volume label:
File system: NTFS
Available space to current user: 15114977280 bytes
Total available space: 15114977280 bytes
Total size of drive: 25958948864 bytes
Drive E:\
File type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/

using System;
using System.IO;

class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);

Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);

Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}
}

Detect Windows Clipboard Change

// Detect Windows Clipboard Change

// ...

[DllImport("user32.dll")]
public static extern int SetClipboardViewer(int hwnd);

public Form1() {
InitializeComponent();
SetClipboardViewer(this.Handle.ToInt32());
}

protected override void WndProc(ref Message m) {
base.WndProc(ref m);

// chkClipboard isn't checked initially
// so this prevents MessageBox on Form load:

if (m.Msg == 776 && chkClipboard.Checked)
{
// Clipboard change!
}
}

Displaying HTML Help Files

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DevDistrict
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.HelpProvider helpProvider1;
private System.Windows.Forms.Button btnShowContents;
private System.Windows.Forms.Button btnShowIndex;
private System.Windows.Forms.Button btnSearchHelp;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

private void btnShowContents_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this, helpProvider1.HelpNamespace);
}

private void btnShowIndex_Click(object sender, System.EventArgs e)
{
Help.ShowHelpIndex(this, helpProvider1.HelpNamespace);
}

private void btnSearchHelp_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this, helpProvider1.HelpNamespace, HelpNavigator.Find, "");
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.helpProvider1 = new System.Windows.Forms.HelpProvider();
this.btnShowContents = new System.Windows.Forms.Button();
this.btnShowIndex = new System.Windows.Forms.Button();
this.btnSearchHelp = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// helpProvider1
//
this.helpProvider1.HelpNamespace = "C:\\dev\\WindowsApplication40\\htmlhelp.chm";
//
// btnShowContents
//
this.btnShowContents.Location = new System.Drawing.Point(8, 8);
this.btnShowContents.Name = "btnShowContents";
this.btnShowContents.Size = new System.Drawing.Size(96, 23);
this.btnShowContents.TabIndex = 0;
this.btnShowContents.Text = "Show Contents";
this.btnShowContents.Click += new System.EventHandler(this.btnShowContents_Click);
//
// btnShowIndex
//
this.btnShowIndex.Location = new System.Drawing.Point(8, 48);
this.btnShowIndex.Name = "btnShowIndex";
this.btnShowIndex.Size = new System.Drawing.Size(96, 23);
this.btnShowIndex.TabIndex = 1;
this.btnShowIndex.Text = "Show Index";
this.btnShowIndex.Click += new System.EventHandler(this.btnShowIndex_Click);
//
// btnSearchHelp
//
this.btnSearchHelp.Location = new System.Drawing.Point(8, 88);
this.btnSearchHelp.Name = "btnSearchHelp";
this.btnSearchHelp.Size = new System.Drawing.Size(96, 23);
this.btnSearchHelp.TabIndex = 2;
this.btnSearchHelp.Text = "Search Help";
this.btnSearchHelp.Click += new System.EventHandler(this.btnSearchHelp_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(200, 174);
this.Controls.Add(this.btnSearchHelp);
this.Controls.Add(this.btnShowIndex);
this.Controls.Add(this.btnShowContents);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
}

}

Get OS version

// Get OS version

public static string GetMachineOS()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (Environment.OSVersion.Version.Major<=4)
return String.Format("Windows NT {0}", Environment.OSVersion.Version.ToString());
if (Environment.OSVersion.Version.Major==5)
{
if (Environment.OSVersion.Version.Minor==0)
return String.Format("Windows 2000 {0}", Environment.OSVersion.Version.ToString());
else
return String.Format("Windows XP {0}", Environment.OSVersion.Version.ToString());
}
}

if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
{
if (Environment.OSVersion.Version.Major>=4)
{
if (Environment.OSVersion.Version.Minor==0)
return String.Format("Windows 95 {0}", Environment.OSVersion.Version.ToString());
else if (Environment.OSVersion.Version.Minor<90)
return String.Format("Windows 98 {0}", Environment.OSVersion.Version.ToString());
else
return String.Format("Windows Millenim Edition {0}", Environment.OSVersion.Version.ToString());
}
}

return Environment.OSVersion.ToString(); //ELSE
}

Get list of running processes

// Get list of running processes

Console.WriteLine("ID:\tProcess name:");
Console.WriteLine("--\t------------");
foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
Console.WriteLine("{0}\t{1}", process.Id, process.ProcessName);

How to Monitor a Process

using System;
using System.Windows.Forms;

namespace DevDistrict.MachineMonitor
{
public class Monitor
{
[STAThread()]
public static void Main()
{
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("Notepad");

if (p.Length<0)
{
Console.WriteLine("No process");
return;
}

if (p[0].HasExited)
{
Console.WriteLine("Process Exited");
return;
}

p[0].Exited+=new EventHandler(Startup_Exited);

while(!p[0].HasExited)
{
p[0].WaitForExit(30000);

Console.WriteLine("checking process health");
}
}

private static void Startup_Exited(object sender, EventArgs e)
{
Console.WriteLine("PROCESS EXIT CAUGHT");
}
}
}

All Menu