I am trying to enable IIS features through c# console application. It is working fine in windows 7 and windows 8.1 machines. but when I run the same code on windows server 2008 R2 and windows server 2012 R2 it is not working. What am I missing in this code?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
namespace EnableIISComponents
{
class Program
{
static void Main(string[] args)
{
try
{
SetupIIS();
Console.WriteLine("Done. Press any key to close.");
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred:" + ex.Message);
}
Console.ReadLine();
}
static string SetupIIS()
{
// In command prompt run this command to see all the features names which are equivalent to UI features.
// c:\>dism /online /get-features /format:table
var featureNames = new List<string>
{
"IIS-ApplicationDevelopment",
"IIS-ISAPIExtensions",
"IIS-ISAPIFilter",
"IIS-CommonHttpFeatures",
"IIS-DefaultDocument",
"IIS-HttpErrors",
"IIS-StaticContent",
"IIS-HealthAndDiagnostics",
"IIS-HttpLogging",
"IIS-HttpTracing",
"IIS-WebServer",
"IIS-WebServerRole",
"IIS-ManagementConsole",
};
Console.WriteLine("Checking the Operating System...\n");
ManagementObjectSearcher obj = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
try
{
foreach (ManagementObject wmi in obj.Get())
{
string Name = wmi.GetPropertyValue("Caption").ToString();
// Remove all non-alphanumeric characters so that only letters, numbers, and spaces are left.
// Imp. for 32 bit window server 2008
Name = Regex.Replace(Name.ToString(), "[^A-Za-z0-9 ]", "");
if (Name.Contains("Server 2012 R2") || Name.Contains("Windows 81"))
{
featureNames.Add("IIS-ASPNET45");
featureNames.Add("IIS-NetFxExtensibility45");
}
else if (Name.Contains("Server 2008 R2") || Name.Contains("Windows 7"))
{
featureNames.Add("IIS-ASPNET");
featureNames.Add("IIS-NetFxExtensibility");
}
else
{
featureNames.Clear();
}
string Version = (string)wmi["Version"];
string Architecture = (string)wmi["OSArchitecture"];
Console.WriteLine("Operating System details:");
Console.WriteLine("OS Name: " + Name);
Console.WriteLine("Version: " + Version);
Console.WriteLine("Architecture: " + Architecture + "\n");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred:" + ex.Message);
}
return Run(
"dism",
string.Format(
"/NoRestart /Online /Enable-Feature {0}",
string.Join(
" ",
featureNames.Select(name => string.Format("/FeatureName:{0}", name)))));
}
static string Run(string fileName, string arguments)
{
Console.WriteLine("Enabling IIS features...");
Console.WriteLine(arguments);
using (var process = Process.Start(new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false,
}))
{
process.WaitForExit();
return process.StandardOutput.ReadToEnd();
}
}
}
}
Instead of running this program on server machine If I run the dism command with same feaureNames through command prompt, the IIS features are getting enabled. Why not it is working with the program?
I am running my program as administrator by right clicking and selecting "run as administrator".
I have created this sample taking help from this link. Better way to install IIS7 programmatically
Finally I found the cause. In project properties target platform is "Any CPU" but "Prefer 32-bit" option was checked. This causing the problem. In server 2012 R2 my application trying to use 32bit version of DISM. That is causing the problem.
Once I unchecked that option. It is working fine.