Click here to Skip to main content
15,896,207 members
Articles / Web Development / IIS

Programmatically Manage IIS

Rate me:
Please Sign up or sign in to vote.
4.48/5 (12 votes)
27 Nov 2008CPOL5 min read 74.3K   1.8K   55  
Base library for programmatically managing IIS through C# and Directory Services.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sarafian.Framework.General.DirectoryManager.IIS;
using Sarafian.IISTreeView.TreeNodes;
using System.IO;

namespace Sarafian.IISTreeView
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void tvEntries_AfterSelect(object sender, TreeViewEventArgs e)
        {
            BaseNode bn = e.Node as BaseNode;
            if (bn != null)
            {
                GridFromBaseNode(bn);
            }

            DirectoryEntryNode den = e.Node as DirectoryEntryNode;
            if (den != null)
            {
                GridFromDirectoryEntryNode(den);
            }
        }

        private void GridFromDirectoryEntryNode(DirectoryEntryNode den)
        {
            this.dgvProperties.DataSource = den.Properties;
        }

        private void GridFromBaseNode(BaseNode bn)
        {
            this.dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dgvProperties.AutoGenerateColumns = true;
            this.dgvProperties.DataSource = bn.Properties;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dgvProperties.AutoGenerateColumns = true;
            this.service = new Service("LocalHost");
            base.Text = String.Format("{0} IIS {1}.{2}", this.service.ServiceAddress, this.service.MajorVersionNumber, this.service.MinorVersionNumber);
            RefreshTree();
        }

        private Service service;
        private void RefreshTree()
        {
            this.tvEntries.Nodes.Clear();
            this.tvEntries.Nodes.Add(new ServiceNodeEntry(this.service));
        }

        private void btnNewRandomSite_Click(object sender, EventArgs e)
        {
            //int port = new Random().Next(1000, 10000);
            int port = 8030;
            string siteName = "ECMTest";// +port.ToString();
            string sitePath = Path.Combine(@"C:\WebSites", siteName);
            //string sitePath = Path.Combine(@"D:\Web Sites", siteName);
            WebSite ws = service.AddWebSite(siteName, port, sitePath);

            string vdPath = Path.Combine(sitePath, "Server");
            ws.ConvertToApplication("Server");
            RefreshTree();
            MessageBox.Show(siteName);

            //foreach (System.DirectoryServices.DirectoryEntry de  in service.Children)
            //{
            //    int siteID;
            //    if (Int32.TryParse(de.Name, out siteID))
            //    {
            //        if (siteID>=562&&siteID<=568)
            //        {
            //            de.DeleteTree();
            //        }
            //    }
            //}
            //service.CommitChanges();
            //RefreshTree();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshTree();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            WebSiteNodeEntry ws = this.tvEntries.SelectedNode as WebSiteNodeEntry;
            if (ws == null)
            {
                return;
            }
            ((WebSite)ws.Entry).Start();

        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            WebSiteNodeEntry ws = this.tvEntries.SelectedNode as WebSiteNodeEntry;
            if (ws == null)
            {
                return;
            }
            ((WebSite)ws.Entry).Stop();

        }
        private void btnRestart_Click(object sender, EventArgs e)
        {
            WebSiteNodeEntry ws = this.tvEntries.SelectedNode as WebSiteNodeEntry;
            if (ws == null)
            {
                return;
            }
            ((WebSite)ws.Entry).Restart();

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader ALGOSYSTEMS
Greece Greece
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies

I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.

I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.

I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Comments and Discussions