Monthly Archives: November 2012

Find classes that implement an interface or that are descendants of a given class in C#

When you enter a class or interface name and click Find, this example uses the following code to search the currently loaded assemblies for classes that are descendants of the class or interface. private void btnFind_Click(object sender, EventArgs e) { … Continue reading

Posted in classes, inheritance, interfaces, reflection | Leave a comment

List a program’s loaded assemblies in C#

The key to this example is the following ListAssemblies method. // List the assemblies. private void ListAssemblies() { lblNumAssemblies.Text = “”; lstAssemblies.Items.Clear(); Cursor = Cursors.WaitCursor; Refresh(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { lstAssemblies.Items.Add(assembly.GetName().Name); } // Display the number of assemblies. … Continue reading

Posted in classes, reflection | Leave a comment

Get the computer’s registered owner and organization in C#

In Windows 98, this information is stored in the Registry keys: SOFTWARE\Microsoft\Windows\CurrentVersion\RegisteredOwner SOFTWARE\Microsoft\Windows\CurrentVersion\RegisteredOrganization In Windows NT (and apparently later), this information is stored in the Registry keys: SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwner SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization The program uses the following code to get the … Continue reading

Posted in registry, system | Leave a comment

Get the computer’s operating system in C#

This example uses the following code to display the operating system version when it starts. private void Form1_Load(object sender, EventArgs e) { OperatingSystem os_info = System.Environment.OSVersion; lblOs.Text = os_info.VersionString; } This example simply gets the System.Environment.OSVersion object and displays its … Continue reading

Posted in system | Leave a comment

Write values to the Registry (and use that to refresh the desktop icons) in C#

The example Read Registry values (to get the desktop icon size) in C# shows how to read values from the Registry. This example shows how to write values into the Registry and uses that ability to refresh the desktop icons … Continue reading

Posted in registry, system | Leave a comment

Read Registry values (to get the desktop icon size) in C#

To make using Registry methods easier, this example defines a RegistryTools class that contains static methods for interacting with the Registry. The following code shows the GetRegistryValue method. // Get a registry value. public static object GetRegistryValue(RegistryKey hive, string subkey_name, … Continue reading

Posted in registry, system | Leave a comment

Define custom exception classes in C#

The example Throw standard exceptions in C# explains how to throw exceptions to tell the program about unexpected errors and the example See a hierarchy of useful exception classes for use in C# lists some useful exception classes. But what … Continue reading

Posted in coding | Leave a comment

See a hierarchy of useful exception classes for use in C#

The example Throw standard exceptions in C# explains how to throw exceptions to indicate a problem in a program. Throwing an exception is easiest if you can find an existing exception class that correctly represents your situation. The following list … Continue reading

Posted in coding | 2 Comments

Throw standard exceptions in C#

The example Use try catch blocks to protect against unexpected errors in C# explains how to catch exceptions and briefly mentions that you can use the throw statement to throw your own exceptions. This example shows how to throw standard … Continue reading

Posted in coding | Leave a comment

Make a program that simulates rolling two 6-sided dice and compares the results to expected results in C#

(This example is for a book I’m just starting on. I’ll post information on it in a few months when it’s closer to publication.) When you enter a number of trials and click Roll, the program uses the following code … Continue reading

Posted in algorithms, graphics, mathematics | Leave a comment