View C# - Get name of currently active

Authored in 2011

The following code can be copy pasted directly into a C# class. Calls to getCurrentAppName() will return a string with the name of the current application.

        #region "WinAPI"

        private static extern IntPtr GetForegroundWindow();
View Saving XDocument to itself in

Authored in 2010

While working with the LINQ XDocument object, I was attempting to modify the XML file loaded into the XDocument and then save it to itself. And while XDocument has a Save method, it needs a file path parameter. By default, XMLDocument would save to itself if no file path parameter was passed, but not so with XDocument.

Instead, you have to retrieve the file path of the loaded XML file using the BaseURI property of XDocument. The BaseURI property contains a URI (Uniform Resource Identifier), which essentially is a file path, to the "base", i.e. The loaded document. To save the XDocument to itself, you'd then think that all you need would be:

Dim xdoc As XDocument = XDocument.Load("C:\Test\Test.xml")
View Getting description from a process in

Authored in 2010

The process object contains a lot of different information about the process in question, however it lacks the "description" of a process.

To see the description of a given process, hit CTRL+ALT+Delete and start the task manager. From there navigate to "Processes" and look under the "Description" column on the far right. This description is typically the easiest way to identify an application. You would think the process name would be the most obvious choice to identify an application, but this name is not always obvious.

For example, the Visual Studio 2010 process is called "devenv". Not very telling. The description of the process on the other hand is much more usable: "Microsoft Visual Studio 2010". That one doesn't leave much to the imagination, and typically this is what you are after if you are trying to identify an application based on a process.

As mentioned the process object lacks the description of a process. This is because the description is actually not stored in the proce

View Capture process information from the active application in

Authored in 2010

Capturing process information from the currently active application can be useful when coding interactions between applications.

The code for getting this information is relatively simple, though it involves a few different Windows API calls.

First we need to declare the Windows API functions, which are done as follows. Enter the following code in a class of your choice:

View Show form on top of desktop with WinAPI and

Authored in 2010

Showing a form above the desktop but below ordinary applications, can be a useful method when trying to develop an application that feels integrated to the Windows UI. This could typically be desktop gadgets / widgets and similar. You want a form that is shown always, even when users perform a "Minimize all" command.

Add the following class to your project:

Public Class WinDesktop
View How to add global hotkeys to applications in

Authored in 2010

When you want to make your application do things by the user pressing a hotkey / key combination, even when your application does not have focus, you will need a so called global hotkey. A global hotkey is a form of "keyboard hook" which is a low level way of monitoring the computer for a certain key combination, aka hotkey. Below is a simple class, which let's you register a hotkey and define what code should execute, once the hotkey is pressed.

    Public Class Hotkey

#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
View VB.NET Winforms - Making form with formBorderStyle=none

Authored in 2010

When building UI's for desktop applications in Winforms, you occassionally need full graphical control of the form. The default form with a title bar is thus not suitable. Setting the formBorderStyle property of forms to none, means it no longer has a top title bar, thereby giving full graphical control of the window.

A common problem with this however, is that the user can no longer move the form by dragging it's titlebar around the screen (as the titlebar has been hidden).

The following code is a workaround that allows forms to be dragged around, like with any usual application. It has to be entered in the code-behind for the form for which you want to enable dragging.