Click here to Skip to main content
Click here to Skip to main content

Custom Control using C# For Virtual Directory in IIS

, 16 Dec 2007
Rate this:
Please Sign up or sign in to vote.
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.
Screenshot - form.jpg

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

Screenshot - VirtualDirClass.png 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.

Screenshot - Properties1.jpg

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:

Screenshot - defaultdoc.jpg

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:

Screenshot - aspnetversion1.jpg

Using the following methods, we can create, delete Virtual Directory in IIS and we can use it to open webpage in IIS using that:

Screenshot - AllMethods1.jpg

Using the Code

// This is the simple code from where we are using the Control
VirtualDirLib Vdir = new VirtualDirLib(); // Create Object 
Vdir.DefaultDoc = "index.html";           //Set Default page
Vdir.AuthNTLM = false;                    // Set Security
Vdir.CreateVDir(System.Environment.MachineName, "D:\\Myweb", "Sample");
Vdir.SetAspNetVersionIIS();               // Set ASP.NET version
Vdir.InvokeVDir();                        // Run Web page
//Some Property  Settings
/// <span class="code-SummaryComment"><summary></span>
/// Virtual Directory Class
/// <span class="code-SummaryComment"></summary></span>

[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 

/// <span class="code-SummaryComment"><summary></span>
/// Provide Access Write To VirtulDirectory
/// <span class="code-SummaryComment"></summary></span>
[Description("Set AccessWrite Permission")]
public bool AccessWrite
{
get { return accessWrite; }
set { accessWrite = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Execute Permission To Virtual Directory
/// <span class="code-SummaryComment"></summary></span>
[Description("Set AccessExecute Permission")]
public bool AccessExecute

{
get { return accessExecute; }
set { accessExecute = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Allow Directory Browsing
/// <span class="code-SummaryComment"></summary></span>
[Description("Set Directory Browsing Permission")]
public bool DirBrowsing
{
get { return dirBrowsing; }
set { dirBrowsing = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Set The Default pages like index.html
/// <span class="code-SummaryComment"></summary></span>
[Description("Set Default Documents")]
public string DefaultDoc
{
get { return defaultDoc; }
set { defaultDoc = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Enable the default document settings
/// <span class="code-SummaryComment"></summary></span>
[Description("Enabled Default Documents")]
public bool EnableDefaultDoc
{
get { return enableDefaultDoc; }
set { enableDefaultDoc = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Set Anonymous Authentication True or False
/// <span class="code-SummaryComment"></summary></span>
[Description("Set Anonumus Authontication mode")]
public bool AuthAnonumus
{
get { return authAnonumus; }
Set { authAnonumus = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Set Basic Authontication True or False
/// <span class="code-SummaryComment"></summary></span>
[Description("Set Basic Authontication")]

public bool AuthBasic
{
get { return authBasic; }
set { authBasic = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Set Integrated Windows Authentication True or False
/// <span class="code-SummaryComment"></summary></span>
[Description("Set Integrated Authontication mode")]
public bool AuthNTLM
{
get { return authNTLM; }
set { authNTLM = value; }
}

/// <span class="code-SummaryComment"><summary></span>
/// Give the Read Access Permission 
/// By Default : True
/// <span class="code-SummaryComment"></summary></span>
[Description("Set AccessRead Permission")]
public bool AccessRead
{
get { return accessRead; }
set { accessRead = value; }
}

// Code For ASP.NET Version Set
/// <span class="code-SummaryComment"><summary> </span>
/// It will Changes The Scriptmaps and Set IIS ASP.NET Version 
/// <span class="code-SummaryComment"></summary> </span>
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.

License

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

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband
 
Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
QuestionNeed Help Pinmemberkrishnamzp9-Sep-09 21:19 
GeneralIIS 6 on Vista Ultimate 64 bit PinmemberFrankenstien225-Dec-08 8:34 
GeneralRe: IIS 6 on Vista Ultimate 64 bit PinmemberAbhijit Jana25-Dec-08 18:26 
GeneralNo sample PinmemberAazzin6-Apr-08 14:56 
GeneralRe: No sample PinmemberAbhijit Jana6-Apr-08 18:35 
Please check the XML Documentation , that is sufficient to understand each methods and properties.
 
Thanks
 
Best Regards
-----------------
Abhijit Jana
Microsoft Certified Professional
"Success is Journey it's not a destination"

GeneralRe: No sample PinmemberAazzin7-Apr-08 8:25 
GeneralRe: No sample PinmemberAazzin7-Apr-08 8:56 
Generalicon change for components !! PinmemberAbhijit Jana13-Nov-07 4:58 
GeneralRe: icon change for components !! PinmemberN a v a n e e t h15-Nov-07 20:51 
GeneralRe: icon change for components !! PinmemberAbhijit Jana16-Nov-07 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 17 Dec 2007
Article Copyright 2007 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid