Posts

Showing posts from January, 2006

Loading an Image from an Embedded Resource in .NET

Disclaimer: The following information is for educational purposes only. Do not use to harass muppets. This is one of those things that I learned to do back when I first started learning .NET, but can never remember how to do when I need it, so I'm adding it here. Hopefully this will come in handy to someone. First, add an image to your project. You can do this by right-clicking on your project in the Solution Explorer and selecting Add->Add Existing Item , then browse for an image file using the dialog that appears. After the image has been added, click on it in the Solution Explorer and set its Build Action to Embedded Resource . Adding images to your project this way is useful for when you deploy your assembly -- it removes the need to distribute individual image files, instead embedding them into your assembly. Next, use the following line of code to load your image from the embedded resource (examples are given in both C# and VB.NET. Please note that the below examples

Find a Running Process in .NET

Disclaimer: The sample code below is for educational purposes only. Do not rub on skin. A few years back I wrote a class module in VB6 to allow an application to detect whether a specified process is currently running on the user's system. This required a lot of API code, and, while it gets the job done, it's a bit unwieldly, especially if you're debugging an app that uses it and you end up stepping through it's code rather than stepping over it. But there's a much easier way to do it in .NET, with only a few lines of code. The answer lies in the System.Diagnostics namespace, and the Process class contained within. Why they put the Process class in the Diagnostics namespace is beyond me -- sure, you can use it for diagnostics, but also for, among other things, executing processes. But for this example, we'll focus simply on determining if a specified process is running. First, you'll need to add a reference to the aforementioned System.Diagnostics na

Custom C# InputBox Class

Disclaimer: The following source code is for educational purposes only. Please do not use for evil. I have a small project in C# which I use for testing out various things I find when going through the .NET namespaces. It's a good way to learn new things, and on more than one occasion I've tinkered with something that came up later in a work-related project, thereby saving me the time it would take to learn it on-the-job. Anyway, I was working with this app the other day and was reminded of something when I needed to prompt the user for a string: C# does not have an InputBox function/class. So I wrote my own. This is a very simple example, but just because it's simple doesn't mean it's not useful. First, create a new form, identical to the standard InputBox that VB has. Give it a label for the message to the user, a text box for user input, and two buttons, one labeled "OK", the other "Cancel". Set the form's AcceptButton property to the OK b

Cool Programming Functionality: Windows Management Instrumentation (Part Two)

As a follow-up to my previous post, here is some sample code in C# which demonstrates how to use Windows Management Instrumentation (WMI) to gather information on a system's logical drives. The code is from a class I am working on ( in a personal project, not work related ) which will eventually contain several useful functions pertaining to file and drive operations. Eventually, I would also like the function in this code to return the caption of the device (for example, Memorex 2.4x DVD Writer ). using System; using System.Management; namespace AlsFileSystemTools {    public enum DriveType     {       NoRoot = 1,       Removable = 2,       LocalDisk = 3,       Network = 4,       CDorDVD = 5,       RAMDrive = 6     };     public struct Drive     {       public string Letter;       public string Description;       public int       DriveType;       public string VolumeName;       public string StatusInfo;     }     ///     /// A collection of functions used to