C#: Loading Dynamic Assemblies Into Other AppDomains

So you have generated a dynamic assembly (like here) and want to load the assembly into a new (or at least another than the current) AppDomain? Well, there’s a catch. You cannot share dynamic assemblies across AppDomains (easily)… Continue reading

C#: Test For Elevated Privileges

So you want to know if your current code is has been “run as administrator”? You’ll need to check if the current Windows User has the built-in role “Administrator”. To make things short, here you go:

using System.Security.Principal;

// ...

var windowsIdentity = WindowsIdentity.GetCurrent();
var windowsPrincipal = new WindowsPrincipal(windowsIdentity );
var hasElevatedPrivileges = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);

Modify Web Platform Installer Configuration with C#

Suppose you intend to call the Web Platform Installer (WPI) from C# code or from the PowerShell. Suppose you want to provide a custom feed or want to change the address of the remote db server and display WPI to the user to perform an installation. Unfortunately, you cannot supply configuration parameters to the UI version of WPI (WebPlatformInstaller.exe). The command-line version (WebPiCmd.exe) can be supplied with all necessary parameters, but does not display a nice UI to the user so he\she can conveniently configure the installed application.

This post shows how to modify the WPI preferences file using C#. This file contains the configuration of the UI version of Web Platform Installer. Continue reading

Dynamically Determine MIME Types in C#

Suppose you need to determine the MIME type for a given file extension in your code. Since Windows already knows many MIME types and since these MIME type are constantly updating, it seems a good idea to use Windows’ data instead of including a table of MIME types into your code by yourself. Continue reading

A Flexible And Convenient Type Conversion Helper in C#

Every once in a while I need to convert objects of an unknown type into a known type; sometimes these types are only known at run time. In most cases I want the code to stay easy to read, so I don’t want to if-then-else the readybility of my code away.
Over the time, I have created a helper that performs the conversion in a convenient and fast way. Use it if you like.
Continue reading