Posts

Simulating VB's App.PrevInstance in VB.NET and C#

Disclaimer: The following information is for educational uses only. Please do not use to leak classified information. Versions of Visual Basic prior to VB.NET had an App object (not to be confused with .NET's Application object) which exposed some useful functionality, including the PrevInstance function, which indicated whether or not a previous instance of the application was currently running. This function does not exist in VB.NET, but the functionality itself is present. This posting will show you how to use it, both in VB.NET and C#. To determine if a previous version of the application is running, we can use the Process object contained within the System.Diagnostics namespace. Specifically, the Process object's GetProcessesByName() method provides the functionality we need. Take a look at the C# code below (a using System.Diagnostics; declaration is implied): public int iGetProcessCount( string ProcessName ) {    Process[] proc = null;    try ...

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     {  ...

Cool Programming Functionality: Windows Management Instrumentation

In one of the first .NET apps I wrote, I needed to be able to cycle through the CD/DVD drives on the user's system. To determine which logical drives were, in fact, CD/DVD drives, I used the old scripting DLL that came with VB6, available in .NET via a COM wrapper. I am now re-writing this app in C#, and of course wanted to remove the need for this old DLL. I found the solution in the System.Management namespace, which includes functions for the Windows Management Instrumentation infrastructure. Here is some sample code which loops through a system's logical drives and displays information on each. The sample is in VB.NET and requires a reference to the System.Management namespace. Dim mgmtDisks As New ManagementClass("Win32_LogicalDisk") Dim moc As ManagementObjectCollection = mgmtDisks.GetInstances() Dim mo As ManagementObject For Each mo In moc    Console.WriteLine("Disk type : {0}", mo("DriveType").ToString()) Next mo mgmtDisks.Dispose()

Cool Programming Functionality: Reflection

In Microsoft's .NET Framewowk, Reflection describes the ability to interpret types dynamically at runtime, or, as the documentation puts it: "The classes in the Reflection namespace, together with System.Type, allow you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types. You can also use reflection to create type instances at run time, and to invoke and access them." I've been making use of this lately, and wondering how I could have solved certain tasks without it. Here's a quick example function in C# that takes a namespace name, class name, and method name as parameters, then instantiates the specified class and calls the specified method. This is just a quickie, so I'm forgoing a lot of things like error handling, etc. The code itself is mine, but the architecture behind it is from a colleague of mine named Bill Butler ( Xcent ). public void ExecObjectMethod( string NamespaceName, ...