Introduction
This is a Windows based Custom control to Create, Delete Virtual Directory in IIS and we can set all properties like default page, ASP.NET version, Authentication Type through it. Sometimes it is very difficult to create a virtual directory in IIS to deploy a Web site using codes. This is a very simple control using which we can create as well as set all other properties of IIS. We can also run the webpage directly from the control.
Background
For Creating Virtual Directory in IIS we need System.DirectoryServices.dll. We need System.ServiceProcess.dll for interacting with IIS services.
This is a Custom Control that inherits from Components.
Designing
 | This custom control is totally based on some methods and properties. If we check the Class Diagram of the Virtual Directory class, it has some methods like CreateVDir() , Delete() , StartIIS() , StopIIS() , and some properties like AccessRead , AccessWrite , etc. |
Now we can simply create an object of the Control
class and create the Virtual Directory.
By default, all properties are set to true
and the default page should be default.aspx. We can change all these properties in runtime too.
If we want to change the default document, that means if our first page is index.html, we can use it in this way. This will set the default page corresponding to that Virtual Directory:
ASP.NET Version Change : One of the main parts of Web page deployment is to assign the current ASP.NET version and we can do it very simply using the following method:
Using the following methods, we can create, delete Virtual Directory in IIS and we can use it to open webpage in IIS using that:
Using the Code
VirtualDirLib Vdir = new VirtualDirLib();
Vdir.DefaultDoc = "index.html";
Vdir.AuthNTLM = false;
Vdir.CreateVDir(System.Environment.MachineName, "D:\\Myweb", "Sample");
Vdir.SetAspNetVersionIIS();
Vdir.InvokeVDir();
[System.Drawing.ToolboxBitmap("vdir.ico")]
public partial class VirtualDirLib : Component
{
#region PropertyVariable
private bool accessRead=true;
private bool accessWrite=true;
private bool accessExecute=true;
private bool dirBrowsing=true;
private string defaultDoc="Default.aspx";
private bool enableDefaultDoc=true;
private bool authBasic=false;
private bool authNTLM=false;
private bool authAnonumus=true;
#endregion
[Description("Set AccessWrite Permission")]
public bool AccessWrite
{
get { return accessWrite; }
set { accessWrite = value; }
}
[Description("Set AccessExecute Permission")]
public bool AccessExecute
{
get { return accessExecute; }
set { accessExecute = value; }
}
[Description("Set Directory Browsing Permission")]
public bool DirBrowsing
{
get { return dirBrowsing; }
set { dirBrowsing = value; }
}
[Description("Set Default Documents")]
public string DefaultDoc
{
get { return defaultDoc; }
set { defaultDoc = value; }
}
[Description("Enabled Default Documents")]
public bool EnableDefaultDoc
{
get { return enableDefaultDoc; }
set { enableDefaultDoc = value; }
}
[Description("Set Anonumus Authontication mode")]
public bool AuthAnonumus
{
get { return authAnonumus; }
Set { authAnonumus = value; }
}
[Description("Set Basic Authontication")]
public bool AuthBasic
{
get { return authBasic; }
set { authBasic = value; }
}
[Description("Set Integrated Authontication mode")]
public bool AuthNTLM
{
get { return authNTLM; }
set { authNTLM = value; }
}
[Description("Set AccessRead Permission")]
public bool AccessRead
{
get { return accessRead; }
set { accessRead = value; }
}
public void SetAspNetVersionIIS()
{
try
{
string _frameWorkDir;
string _Dir;
string _FrameWorkVersion = Environment.Version.ToString();
_Dir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
int dirpos = _Dir.Remove(_Dir.Length - 1, 1).LastIndexOf(@"\");
_frameWorkDir = _Dir.Remove(dirpos, _Dir.Length - dirpos);
_frameWorkDir = _frameWorkDir + @"\v" + _FrameWorkVersion + @"\";
Process pro = new Process();
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.FileName = _Dir + "aspnet_regiis";
pro.StartInfo.Arguments = @"-s " + @"/W3SVC/1/Root/" + _strServerName;
pro.Start();
pro.WaitForExit();
}
catch (Exception frm)
{
MessageBox.Show(frm.Message);
}
}
References
Points of Interest
I used XML documentation at the time of creating this control, so that it is very understandable to the user and it's very simple to use.