const string isAPI_partialPath = @"v4.0.30319\aspnet_isapi.dll"; static void Main(string[] args) { ServerManager svrMgr = new ServerManager(); Configuration config = svrMgr.GetApplicationHostConfiguration(); ConfigurationSection section = config.GetSection("system.webServer/security/isapiCgiRestriction"); foreach (ConfigurationElement item in section.GetCollection())//这里是GetCollection(),不是ChildElement。诸如<add />的标签是集合不是它的子元素 { if (item.Attributes.Count > 0 && item.Attributes["path"].Value != null && item.Attributes["path"].Value.ToString().EndsWith(isAPI_partialPath)) { item.Attributes["allowed"].Value = true; } } svrMgr.CommitChanges(); }