尝试这个:
// Create the Server Manager Object:
ServerManager defaultManager = new ServerManager();
// Add the Application-Pool:
ApplicationPool defaultAppPool = defaultManager.ApplicationPools.Add("DefaultAppPool");
// Configure the Pool to Automatically Start.
defaultAppPool.AutoStart = true;
// If IIS Application-Pool Exceeds the CPU Limit Property:
defaultAppPool.Cpu.Action = ProcessorAction.KillW3wp;
// Pipeline:
defaultAppPool.ManagedPipelineMode = ManagedPipeLineMode.Integrated;
// Set Runtime:
defaultAppPool.ManagedRuntimeVersion = "v2.0";
// User Network Service Account:
defaultAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
// Idle:
defaultAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
// Max Number of IIS Worker Processes: (W3wp)
defaultAppPool.ProcessModel.MaxProcess = 1;
// Commit the Changes:
defaultManager.CommitChanges();
// Dispose:
defaultManager.Dispose();
这可能是因为您没有启动新的 ServerManager / Application-Pool。然后当它去创建用户时;它可能不是可以实际创建用户帐户的帐户。如果您想验证应用程序确实也可以进行此类更改;你可以使用:
WindowsIdentity userIdentity = WindowsIdentity.GetCurrent();
// Test Operating System Version Vista or Greater for UAC
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
return false;
}
else
{
// If UserIdentity came back Null
if (userIdentity == null)
{
throw new InvalidOperationException("Unable to get current user");
}
else
{
// Set Security Principal to ensure user is in proper role.
WindowsPrincipal userPolicy = new WindowsPrincipal(userIdentity);
if (userPolicy.IsInRole(WindowsBuiltInRole.Administrator))
{
return true;
}
else
{
MessageBox.Show("Application isn't in proper administrative user role; please restart.");
return false;
}
}
}