⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iismainwindow.cs

📁 用ADSI操作IIS文件
💻 CS
📖 第 1 页 / 共 3 页
字号:
        {
            IIsWebSite activeSite = _iisAdministrator.WebSites.ActiveWebSite;

            if(activeSite  != null)
            {
                activeSite.Stop();
            }

            if(activeSite == null || site.Index != activeSite.Index)
            {
                site.Start();
            }
        }

        /// <summary>
        /// Handles restoring the program when the notify icon is double clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Restore();
        }

        private void Restore()
        {
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        /// <summary>
        /// Occurs when an item is checked/unchecked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListView_ItemChecked(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                IIsWebSiteListViewItem item = _listView.Items[e.Index] as IIsWebSiteListViewItem;
                if (item != null)
                {
                    item.MakeBold(true);
                    this.UncheckAllOtherItems(item);  
                }
            }
            else if (e.NewValue == CheckState.Unchecked)
            {
                IIsWebSiteListViewItem item = _listView.Items[e.Index] as IIsWebSiteListViewItem;
                if (item != null)
                {
                    item.MakeBold(false);                                      
                }
            }
        }

        /// <summary>
        /// Looks up the associated ListViewItem for the specified web site
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        private IIsWebSiteListViewItem LookupItemForWebSite(IIsWebSite site)
        {
            foreach (IIsWebSiteListViewItem item in _listView.Items)
            {
                if (item.WebSite == site)
                {
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// Occurs when the selected Item changes in the listview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListView_SelectedItemIndexChanged(object sender, EventArgs e)
        {
            if (_listView.SelectedItems.Count > 0)
            {
                IIsWebSiteListViewItem item = _listView.SelectedItems[0] as IIsWebSiteListViewItem;
                
                _buttonStart.Enabled = !item.WebSite.IsActive;
                _buttonStop.Enabled = item.WebSite.IsActive;
                _buttonDelete.Enabled = true;
            }
            else
            {
                _buttonStart.Enabled = false;
                _buttonStop.Enabled = false;
                _buttonDelete.Enabled = false;
            }
        }
        
        /// <summary>
        /// Occurs when someone wants to refresh the available web sites in IIs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRefresh_Clicked(object sender, EventArgs e)
        {
            this.RefreshSites(true);
        }

        /// <summary>
        /// Start the selected site
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonStart_Clicked(object sender, EventArgs e)
        {
            if (_listView.SelectedItems.Count > 0)
            {
                IIsWebSiteListViewItem item = _listView.SelectedItems[0] as IIsWebSiteListViewItem;
                
                IIsWebSite activeSite = _iisAdministrator.WebSites.ActiveWebSite;

                if(activeSite != null)
                {
                    activeSite.Stop();
                }

                item.WebSite.Start();

                // handle new site
            }
        }

        /// <summary>
        /// Stops the selected site
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonStop_Clicked(object sender, EventArgs e)
        {
            if (_listView.SelectedItems.Count > 0)
            {
                IIsWebSiteListViewItem item = _listView.SelectedItems[0] as IIsWebSiteListViewItem;                               

                item.WebSite.Stop();

                // handle new site
            }
        }

        /// <summary>
        /// Occurs when someone wants to create a new web site 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCreate_Clicked(object sender, EventArgs e)
        {
            IIsCreateSiteWindow createSite = new IIsCreateSiteWindow();
            if(createSite.ShowDialog(this) == DialogResult.OK)
            {
                this.RefreshSites(true);
            }
        }

        /// <summary>
        /// Occurs when someone wants to delete the selected site
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonDelete_Clicked(object sender, EventArgs e)
        {
            if (_listView.SelectedItems.Count > 0)
            {
                IIsWebSiteListViewItem item = _listView.SelectedItems[0] as IIsWebSiteListViewItem;

                if(MessageBox.Show("Are you sure you want to delete this site?", "Deletion Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    item.WebSite.Delete();

                    this.RefreshSites(true);
                }
            }
        }

        /// <summary>
        /// Occurs when someone wants to restart iis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRestartIIs_Clicked(object sender, EventArgs e)
        {
            this.BeginRestartIIs();
        }

        /// <summary>
        /// Occurs when someone wants to open iis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonOpenIIs_Clicked(object sender, EventArgs e)
        {
            IIsAdministrator.OpenIIsManagementConsole();
        }

        /// <summary>
        /// Asynchronously restarts iis
        /// </summary>
        private void BeginRestartIIs()
        {
            bool wasVisible = this.Visible;
            if (wasVisible)
            {
                this.Visible = false;
            }

            ConfigureNotifyIcon(false, "Restarting IIS...");

            MethodInvoker d = new MethodInvoker(IIsAdministrator.RestartIIs);
            d.BeginInvoke(new AsyncCallback(this.EndRestartIIs), wasVisible);
        }

        /// <summary>
        /// Called when the thread finishes that is restarting iis
        /// </summary>
        /// <param name="ar"></param>
        private void EndRestartIIs(IAsyncResult ar)
        {            
            bool wasVisible = (bool)ar.AsyncState;
            if (wasVisible)
            {
                this.Visible = true;
            }

            ConfigureNotifyIcon(true);
            
            // technically this is bad, i should marshall this back to the window's thread
            // but the underlying controls seem to do it without problems, so i'm gonna be lazy till it breaks
            this.RefreshSites(true);
        }

        private void ConfigureNotifyIcon(bool enabled)
        {
            ConfigureNotifyIcon(enabled, "IIsAdmin.NET");
        }

        /// <summary>
        /// Configures the notify icon based on its desired state
        /// </summary>
        /// <param name="enabled">Whether or not the icon is enabled</param>
        /// <param name="message">What message to display when the icon is rolled over</param>
        private void ConfigureNotifyIcon(bool enabled, string message)
        {
            _notifyIcon.Text = message;

            if(enabled)
            {
                _notifyIcon.DoubleClick += new EventHandler(this.NotifyIcon_DoubleClick);
                _notifyIcon.ContextMenu = this._contextMenuTray;
            }
            else
            {
                _notifyIcon.DoubleClick -= new EventHandler(this.NotifyIcon_DoubleClick);
                _notifyIcon.ContextMenu = null;
            }
        }
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -