{"action":"closed","issue":{"url":"https://api.github.com/repos/Brucezhang22/IISConfigHelper/issues/1","labels_url":"https://api.github.com/repos/Brucezhang22/IISConfigHelper/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/Brucezhang22/IISConfigHelper/issues/1/comments","events_url":"https://api.github.com/repos/Brucezhang22/IISConfigHelper/issues/1/events","html_url":"https://github.com/Brucezhang22/IISConfigHelper/issues/1","id":60595924,"number":1,"title":"Main winform program","user":{"login":"Brucezhang22","id":11326927,"avatar_url":"https://avatars.githubusercontent.com/u/11326927?v=3","gravatar_id":"","url":"https://api.github.com/users/Brucezhang22","html_url":"https://github.com/Brucezhang22","followers_url":"https://api.github.com/users/Brucezhang22/followers","following_url":"https://api.github.com/users/Brucezhang22/following{/other_user}","gists_url":"https://api.github.com/users/Brucezhang22/gists{/gist_id}","starred_url":"https://api.github.com/users/Brucezhang22/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Brucezhang22/subscriptions","organizations_url":"https://api.github.com/users/Brucezhang22/orgs","repos_url":"https://api.github.com/users/Brucezhang22/repos","events_url":"https://api.github.com/users/Brucezhang22/events{/privacy}","received_events_url":"https://api.github.com/users/Brucezhang22/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-11T01:17:33Z","updated_at":"2015-03-11T01:44:48Z","closed_at":"2015-03-11T01:44:48Z","body":"using System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\nusing Microsoft.Web.Administration;\r\n\r\nnamespace IISConfigHelper\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        ServerManager serverManager = new ServerManager();\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n        #region 查找本地站点\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            listBoxSiteList.Items.Clear();\r\n            foreach (Site site in serverManager.Sites)\r\n            {\r\n                listBoxSiteList.Items.Add(site.Name+\"\\t\\t\"+site.State.ToString());\r\n            }\r\n        }\r\n        #endregion\r\n\r\n        #region 批量添加应用程序池\r\n        private void ButtonConfigAppPool_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0)\r\n            {\r\n                MessageBox.Show(\"请选则要添加应用程序池的站点\");\r\n            }\r\n            else\r\n            {\r\n                if (MessageBox.Show(\"确定修改应用程序池?\", \"提示\", MessageBoxButtons.YesNo) == DialogResult.Yes)\r\n                {\r\n                    foreach (var item in listBoxSiteList.SelectedItems)\r\n                    {\r\n                        string siteName = GetSiteName(item.ToString());\r\n                        ApplicationPool appPool = serverManager.ApplicationPools[siteName];\r\n                        if (appPool == null)\r\n                        {\r\n                            appPool = serverManager.ApplicationPools.Add(siteName);\r\n                            appPool.AutoStart = true;\r\n                            appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;\r\n                            appPool.ManagedRuntimeVersion = \"v4.0\";\r\n                            serverManager.CommitChanges();\r\n                            listBoxLogs.Items.Add(string.Format(\"添加应用程序池：{0}，\\t托管管道模型：集成，\\t.Net Framework版本：v4.0\\t\", siteName));\r\n                        }\r\n                        else\r\n                        {\r\n                            listBoxLogs.Items.Add(string.Format(\"应用程序池{0}已存在，添加失败\", siteName));\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        #endregion\r\n\r\n        #region 批量删除站点\r\n        private void ButtonDeleteSites_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0)\r\n            {\r\n                MessageBox.Show(\"请选则要删除的站点\");\r\n            }\r\n            else\r\n            {\r\n                if (MessageBox.Show(\"确定要删除站点？\", \"提示\", MessageBoxButtons.YesNo) == DialogResult.Yes)\r\n                {\r\n                    foreach (var item in listBoxSiteList.SelectedItems)\r\n                    {\r\n                        string siteName = GetSiteName(item.ToString());\r\n                        Site site = serverManager.Sites[siteName];\r\n                        if (site == null)\r\n                        {\r\n                            listBoxLogs.Items.Add(string.Format(\"站点{0}不存在，删除失败\", siteName));\r\n                        }\r\n                        else\r\n                        {\r\n                            serverManager.Sites.Remove(site);\r\n                            serverManager.CommitChanges();\r\n                            listBoxLogs.Items.Add(string.Format(\"删除站点{0}成功\", siteName));\r\n                        }\r\n                    }\r\n                    listBoxSiteList.Items.Clear();\r\n                    foreach (Site s in serverManager.Sites)\r\n                    {\r\n                        listBoxSiteList.Items.Add(s.Name);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        #endregion\r\n\r\n        #region 窗体初始化\r\n        private void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            panelBinding.Visible = false;\r\n            comboBoxTypes.Items.AddRange(new object[] { \"http\", \"https\", \"net.tcp\", \"net.pipe\", \"net.msmq\", \"msmq.formatname\" });\r\n            listBoxSiteList.Height = 556;\r\n        }\r\n        #endregion\r\n\r\n        #region 显示或隐藏绑定配置区域\r\n        private void button3_Click(object sender, EventArgs e)\r\n        {\r\n            panelBinding.Visible = panelBinding.Visible ? false : true;\r\n        }\r\n        #endregion\r\n\r\n        #region 提交绑定\r\n        private void ButtonConfirm_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0 || comboBoxTypes.SelectedItem == null)\r\n            {\r\n                MessageBox.Show(\"请先选择要配置绑定的站点,并配置正确的绑定信息\");\r\n            }\r\n            else\r\n            {\r\n                string type = comboBoxTypes.SelectedItem.ToString();\r\n                #region Type=http\r\n                if (type == \"http\")\r\n                {\r\n                    string ip = textBoxIP.Text;\r\n                    string port = textBoxPort.Text;\r\n                    string domain = textBoxDomain.Text;\r\n                    if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))\r\n                    {\r\n                        MessageBox.Show(\"请确认协议类型、IP地址、端口输入正确!\");\r\n                    }\r\n                    else\r\n                    {\r\n                        foreach (var item in listBoxSiteList.SelectedItems)\r\n                        {\r\n                            string siteName = GetSiteName(item.ToString());\r\n                            Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                            if (site.Bindings.Where(b => b.Protocol == type && b.BindingInformation == ip + \":\" + port + \":\" + domain).Count() > 0)\r\n                            {\r\n                                listBoxLogs.Items.Add(string.Format(\"站点 {0} 已存在相同配置的绑定\",siteName));\r\n                            }\r\n                            else\r\n                            {\r\n                                site.Bindings.Add(bindingProtocol: type, bindingInformation: ip + \":\" + port + \":\" + domain);\r\n                                listBoxLogs.Items.Add(DateTime.Now.ToString() + \"\\t\" + string.Format(\"为站点 {0} 添加绑定,\\t类型：{1},\\tIP：{2},\\t端口：{3},\\t域名：{4}\", siteName, type, ip, port, domain));\r\n                            }\r\n                            \r\n                        }\r\n                    }\r\n                }\r\n                #endregion\r\n                #region Type=https\r\n                else if (type == \"https\")\r\n                {\r\n                    string ip = textBoxIP.Text;\r\n                    string port = textBoxPort.Text;\r\n                    if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))\r\n                    {\r\n                        MessageBox.Show(\"请确认协议类型、IP地址、端口输入正确!\");\r\n                    }\r\n                    else\r\n                    {\r\n                        foreach (var item in listBoxSiteList.SelectedItems)\r\n                        {\r\n                            string siteName = GetSiteName(item.ToString());\r\n                            Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                            if (site.Bindings.Where(b => b.Protocol == type && b.BindingInformation == ip + \":\" + port + \":\").Count() > 0)\r\n                            {\r\n                                listBoxLogs.Items.Add(string.Format(\"站点 {0} 已存在相同配置的绑定\", siteName));\r\n                            }\r\n                            else\r\n                            {\r\n                                site.Bindings.Add(bindingProtocol: type, bindingInformation: ip + \":\" + port + \":\");\r\n                                listBoxLogs.Items.Add(DateTime.Now.ToString() + \"\\t\" + string.Format(\"为站点 {0} 添加绑定,\\t类型：{1},\\tIP：{2},\\t端口：{3}\", siteName, type, ip, port));\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n                #endregion\r\n                #region 其他Type\r\n                else\r\n                {\r\n                    string bindingInfo = textBoxBindingInfo.Text;\r\n                    if (string.IsNullOrEmpty(bindingInfo))\r\n                    {\r\n                        MessageBox.Show(\"请输入绑定信息!\");\r\n                    }\r\n                    else\r\n                    {\r\n                        foreach (var item in listBoxSiteList.SelectedItems)\r\n                        {\r\n                            string siteName = GetSiteName(item.ToString());\r\n                            Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                            if (site.Bindings.Where(b => b.Protocol == type && b.BindingInformation == bindingInfo).Count() > 0)\r\n                            {\r\n                                listBoxLogs.Items.Add(string.Format(\"站点 {0} 已存在相同配置的绑定\", siteName));\r\n                            }\r\n                            else\r\n                            {\r\n                                site.Bindings.Add(bindingInformation: bindingInfo, bindingProtocol: type);\r\n                                listBoxLogs.Items.Add(DateTime.Now.ToString() + \"\\t\" + string.Format(\"为站点 {0} 添加绑定,\\t类型：{1},\\t绑定信息：{2}}\", siteName, bindingInfo));\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n                #endregion\r\n            }\r\n            serverManager.CommitChanges();\r\n        }\r\n        #endregion\r\n\r\n        #region 绑定配置项Enable或Disable\r\n        private void comboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)\r\n        {\r\n            string type = comboBoxTypes.GetItemText(comboBoxTypes.SelectedItem);\r\n            if (type == \"http\")\r\n            {\r\n                textBoxBindingInfo.Enabled = false;\r\n                textBoxDomain.Enabled = true;\r\n                textBoxIP.Enabled = true;\r\n                textBoxPort.Enabled = true;\r\n\r\n            }\r\n            else if (type == \"https\")\r\n            {\r\n                textBoxDomain.Enabled = false;\r\n                textBoxIP.Enabled = true;\r\n                textBoxPort.Enabled = true;\r\n                textBoxBindingInfo.Enabled = false;\r\n            }\r\n            else\r\n            {\r\n                textBoxDomain.Enabled = false;\r\n                textBoxIP.Enabled = false;\r\n                textBoxPort.Enabled = false;\r\n                textBoxBindingInfo.Enabled = true;\r\n            }\r\n        }\r\n        #endregion\r\n\r\n        #region 删除绑定\r\n        private void ButtonDeleteBinding_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0)\r\n            {\r\n                MessageBox.Show(\"请先选择需要删除绑定的站点！\");\r\n            }\r\n            else\r\n            {\r\n                if (MessageBox.Show(\"此举将会删除所有选中站点的最后一项绑定，请谨慎操作\", \"提示\", MessageBoxButtons.YesNo) == DialogResult.Yes)\r\n                {\r\n                    foreach (var item in listBoxSiteList.SelectedItems)\r\n                    {\r\n                        string siteName = GetSiteName(item.ToString());\r\n                        Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                        Microsoft.Web.Administration.Binding bindingToBeRemoved = site.Bindings.LastOrDefault();\r\n                        if (bindingToBeRemoved == null)\r\n                        {\r\n                            listBoxLogs.Items.Add(DateTime.Now.ToString() + \"\\t\" + string.Format(\"站点 {0} 未设置绑定\", siteName));\r\n                        }\r\n                        else\r\n                        {\r\n                            site.Bindings.Remove(bindingToBeRemoved);\r\n                            listBoxLogs.Items.Add(DateTime.Now.ToString() + \"\\t\" + string.Format(\"站点 {0} 删除绑定，绑定配置：类型：{1}，\\t域名：{2}，\\tIP、端口：{3},\\t绑定信息:{4}\", siteName, bindingToBeRemoved.Protocol, bindingToBeRemoved.Host, bindingToBeRemoved.EndPoint.ToString(), bindingToBeRemoved.BindingInformation));\r\n                        }\r\n                    }\r\n                    serverManager.CommitChanges();\r\n                }\r\n            }\r\n        } \r\n        #endregion\r\n\r\n        #region 启动站点\r\n        private void buttonStartsite_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0)\r\n            {\r\n                MessageBox.Show(\"请先选择你要启动的站点！\");\r\n            }\r\n            else\r\n            {\r\n                foreach (var item in listBoxSiteList.SelectedItems)\r\n                {\r\n                    string siteName = GetSiteName(item.ToString());\r\n                    Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                    if ((site.State != ObjectState.Started) || (site.State != ObjectState.Starting))\r\n                    {\r\n                        try\r\n                        {\r\n                            site.Start();\r\n                            listBoxLogs.Items.Add(string.Format(\"站点 {0} 启动\", siteName));\r\n                        }\r\n                        catch\r\n                        {\r\n                            listBoxLogs.Items.Add(string.Format(\"站点 {0} 启动失败，请检查相关配置\", siteName));\r\n                        }\r\n                    }\r\n                }\r\n                listBoxSiteList.Items.Clear();\r\n                foreach (Site site in serverManager.Sites)\r\n                {\r\n                    listBoxSiteList.Items.Add(site.Name + \"\\t\\t\" + site.State.ToString());\r\n                }\r\n            }\r\n        } \r\n        #endregion\r\n\r\n        #region 工具方法\r\n        private static string GetSiteName(string itemText)\r\n        {\r\n            string[] temp = itemText.Split('\\t');\r\n            return temp[0];\r\n        }\r\n\r\n        #endregion\r\n\r\n        #region 停止站点\r\n        private void buttonStopSite_Click(object sender, EventArgs e)\r\n        {\r\n            if (listBoxSiteList.SelectedItems.Count == 0)\r\n            {\r\n                MessageBox.Show(\"请先选择你要停止的站点！\");\r\n            }\r\n            else\r\n            {\r\n                foreach (var item in listBoxSiteList.SelectedItems)\r\n                {\r\n                    string siteName = GetSiteName(item.ToString());\r\n                    Microsoft.Web.Administration.Site site = serverManager.Sites[siteName];\r\n                    if ((site.State != ObjectState.Stopped) || (site.State != ObjectState.Stopping))\r\n                    {\r\n                        try\r\n                        {\r\n                            site.Stop();\r\n                            listBoxLogs.Items.Add(string.Format(\"站点 {0} 停止\", siteName));\r\n                        }\r\n                        catch\r\n                        {\r\n                            listBoxLogs.Items.Add(string.Format(\"站点 {0} 停止失败，请检查相关配置\", siteName));\r\n                        }\r\n                        \r\n                    }\r\n                }\r\n            }\r\n            listBoxSiteList.Items.Clear();\r\n            foreach (Site site in serverManager.Sites)\r\n            {\r\n                listBoxSiteList.Items.Add(site.Name + \"\\t\\t\" + site.State.ToString());\r\n            }\r\n        } \r\n        #endregion\r\n\r\n    }\r\n}\r\n"}}