At work, we are about to build a new internal tool for administration of different systems. We developers need certain applications and the sales department needs some other ones. All the different people need to be able to administrate different systems, so we decided to create one application that hosts only the applications the individual needs.

Last weekend I played with a generic application that hosts other applications by using reflection. The Plug-in Application does nothing but manages the different plug-ins and let you add and remove plug-ins as you please. When a plug-in is added, it is added to the menu so that every time you open the application, you have easy access to the plug-in.

Each plug-in is in essence a .NET Windows Forms application, but you can also compile the WinForm into a .dll file so it cannot be opened outside the application.

This is what it looks like when the plug-in “TestForm” is running in the application.

Now the plug-in is maximized.

By clicking on the menu item “Manage Plug-ins” it pops up a manager that lets you add and remove plug-ins.

How it works

To make your Windows Forms application compatible with the application, you need to let the main form inherit from a class called PluginForm. The PluginForm resides in its own assembly and is also referenced by the Plug-in Application. It only contains a property called Description and that is used to display tooltips on the menu and in the Plug-in Manager. Your main form needs to inherit from the PluginForm in order to know which form is the one to use in the Plug-in Application.

When you add a plug-in then the Plug-in Application creates an XML document that describes its location, icon, description and name. Because it does not copy the .exe or .dll file but only points to it, means that app.config and other files still work with the plug-in.

This is a simplified version of the one we need at Traceworks where we also needs security and other features. The Plug-in Application is so simple that it is easy to add such features.

Download

The solution contains three projects. The application, the PluginForm base class project and a test plug-in.

PluginApplication.zip (243,15 KB)

Today, I needed to store images in XML files so I had to serialize an image file into text so it could be written in the XML file. Of course, it should also be deserialized back to an image again by reading from the XML file. The result is the following two methods.

The first method serializes a file on disk and returns the base64 string representation.

public static string Serialize(string fileName)
{
 using (FileStream reader = new FileStream(fileName, FileMode.Open))
 {
  byte[] buffer = new byte[reader.Length];
  reader.Read(buffer, 0, (int)reader.Length);
  return Convert.ToBase64String(buffer);
 }
}

The next deserializes a base64 string and writes it to the disk.

public static void DeSerialize(string fileName, string serializedFile)
{
 using (System.IO.FileStream reader = System.IO.File.Create(fileName))
 {
  byte[] buffer = Convert.FromBase64String(serializedFile);
  reader.Write(buffer, 0, buffer.Length);
 }
}