Creating IIS / FTP sites on the fly

Description


This code shows how to Create IIS / FTP sites on the fly

Here is the code:

using System;
using System.DirectoryServices;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, System.EventArgs e)
{
string strSchema = "";
string strRootSubPath = "";
if (radioButton1.Checked)
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
}
if (radioButton2.Checked)
{
strSchema = "IIsFtpVirtualDir";
strRootSubPath = "/MSFTPSVC/1/Root";
}
if (strSchema == "")
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
}

DirectoryEntry deRoot = new DirectoryEntry("IIS://" + txtServer.Text + strRootSubPath);
try
{
deRoot.RefreshCache();
DirectoryEntry deNewVDir = deRoot.Children.Add(txtVDirName.Text, strSchema);
deNewVDir.Properties["Path"].Insert(0, txtVDir.Text);
deNewVDir.CommitChanges();
deRoot.CommitChanges();
// Create a Application
if (strSchema == "IIsWebVirtualDir")
deNewVDir.Invoke("AppCreate", true);
// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
lblStatus.Text = "Virtual Directory " + txtVDirName.Text + "(" + txtVDir.Text + ") has been created";
}
catch (Exception ex)
{
lblStatus.Text = "Error: " + ex.Message;
}
}
}


Also I have attached the code.


Attachments

  • Download demo source example (30572-20546-IISFTPonTheFly.zip)
  • Sample (30572-20644-IISFTPonTheFly.zip)
  • Comments

    Author: Deepika Haridas20 Jul 2009 Member Level: Gold   Points : 1

    Hi,

    Provide description and proper formatting before submitting a resource.

    ---
    Thanks & Regards,
    Deepika
    Editor



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: