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.Read More »

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.
Read More »