If you want to use the same code to create websites on both IIS6 and IIS7 (which I suspect is using the System.DirectoryServices
namespace) then you need to install the IIS6 Management Compatibility features. You can do this by adding this as a Role Service to the Web Server role in Server Manager (under Administrative Tools).
Alternatively you can use the command line on Windows 2008 and Windows 2008R2 (run as Administrator):
Windows 2008/2008R2 -
servermanagercmd -i Web-Mgt-Compat -a
Windows 2008R2 -
ServerManagerCmd.exe
is deprecated and you are advised to use the ServerManager PowerShell module:
import-module ServerManager
add-windowsfeature Web-Mgt-Compat -includeallsubfeature