none
请教关于C#操作应用程序池的问题 RRS feed

  • 问题

  •         int totalServerCount = 0;//站点数量
    
            /// <summary>
            /// 连接字符串
            /// </summary>
            private string _connectString
            {
                get
                { 
                    return "IIS://" + comboxIp.Items[comboxIp.SelectedIndex].ToString() + "/W3SVC";
                }
            }
    
            /// <summary>
            /// 程序池字符串
            /// </summary>
            private string _poolString
            {
                get
                {
                    return _connectString + "/AppPools";
                }
            }
    
            public FormMain()
            {
                InitializeComponent();
                InitApp();
            }
    
            /// <summary>
            /// 初始化
            /// </summary>
            private void InitApp()
            {
                comboxIp.Items.Add("localhost");
                comboxIp.SelectedIndex = 0;
    
                //comboxPoolList.Items.AddRange(enumSite());
                comboxPoolList.Items.AddRange(enumPool());
                if (comboxPoolList.Items.Count > 0)
                {
                    comboxPoolList.SelectedIndex = 0;
                }
                BindListView();
            }
    
            private void BindListView()
            {
                listIIS.Items.Clear();
                listIIS.Columns.Clear();
                listIIS.Columns.Add("索引", 50);
                listIIS.Columns.Add("站点名称", 100);
                listIIS.Columns.Add("状态", 100);
                listIIS.View = View.Details;
                listIIS.FullRowSelect = true;
    
                for (int i = 0; i < enumSite().Length; i++)
                {
                    ListViewItem item = new ListViewItem(i.ToString());
                    item.SubItems.Add(enumSite()[i]);
                    item.SubItems.Add(enumSiteState()[i]);
                    listIIS.Items.AddRange(new ListViewItem[] { item }); 
                }
            }
    
            /// <summary>
            /// 获得站点列表
            /// </summary>
            /// <returns></returns>
            private string[] enumSite()
            {
                List<string> siteName = new List<string>();
                try
                {
                    DirectoryEntry root = GetDirectoryEntry(_connectString);
                    foreach (DirectoryEntry e in root.Children)
                    {
                        if (e.SchemaClassName == "IIsWebServer")
                        {
                            siteName.Add(e.Properties["ServerComment"].Value.ToString());
                        }
                        totalServerCount++;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("程序出错:" + e.Message);
                }
                return siteName.ToArray();
            }
    
            /// <summary>
            /// 获得站点状态列表
            /// </summary>
            /// <returns></returns>
            private string[] enumSiteState()
            {
                List<string> siteState = new List<string>();
                try
                {
                    DirectoryEntry root = GetDirectoryEntry(_connectString);
                    foreach (DirectoryEntry e in root.Children)
                    {
                        if (e.SchemaClassName == "IIsWebServer")
                        {
                            string state = "";
                            switch (e.Properties["ServerState"].Value.ToString())
                            {
                                case "1":
                                    state = "启动中";
                                    break;
                                case "2":
                                    state = "已启动";
                                    break;
                                case "3":
                                    state = "停止中";
                                    break;
                                case "4":
                                    state = "已停止";
                                    break;
                                case "5":
                                    state = "暂停中";
                                    break;
                                case "6":
                                    state = "已暂停";
                                    break;
                                case "7":
                                    state = "进行中";
                                    break;
                            }
                            siteState.Add(state);
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("程序出错:" + e.Message);
                }
                return siteState.ToArray();
            }
    
            private DirectoryEntry GetDirectoryEntry(string path)
            {
                return new DirectoryEntry(path);
            }
    
            /// <summary>
            /// 程序池列表
            /// </summary>
            /// <returns></returns>
            private string[] enumPool()
            {
                List<string> poolList = new List<string>();
                try
                {
                    DirectoryEntry appPools = GetDirectoryEntry(_poolString );
                    foreach (DirectoryEntry a in appPools.Children)
                    {
                        poolList.Add(a.Name);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("程序错误:" + ex.Message);
                }
                return poolList.ToArray();
            }
    
            /// <summary>
            /// 操作程序池
            /// </summary>
            /// <param name="poolName"></param>
            private void ResetAppPool(string poolName)
            {
                try
                {
                    DirectoryEntry pool =GetDirectoryEntry(_poolString);
                    //bool existPool=false;//是否存在
                    DirectoryEntry targetPool = null;
                    foreach(DirectoryEntry a in pool.Children)
                    {
                        if(a.Name==poolName)
                        {
                           // existPool = true;
                            targetPool = a;
                        }
                    }
                   // if (existPool)
                    if(targetPool!=null)
                    {
                        try
                        {
                            pool.Invoke("Stop", null);//Start Stop Recycle
                            MessageBox.Show("回收成功");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        pool.CommitChanges();
                        pool.Close();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show("程序池启动出错:" + ex.Message);
                }
            }
    
            /// <summary>
            /// 退出程序
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            /// <summary>
            /// 回收程序池
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnReset_Click(object sender, EventArgs e)
            {
                string poolName = comboxPoolList.Items[comboxPoolList.SelectedIndex].ToString();
                ResetAppPool(poolName);
            }
        }
    
    以上是我的全部代码。主要作用是获取应用程序池列表,然后回收指定的程序池。但在执行回收时,总是报
    (异常来自 HRESULT:0x80020006 (DISP_E_UNKNOWNNAME))
    我C#水平只是应用,更深入的不了解,望高手指点。谢谢。
    2009年11月22日 15:02

全部回复