Create IIS Virtual Directory in C#


When we are working with C# applications, sometimes we might require to create virtual directory in IIS through the code. In this article we discuss about how to create IIS virtual directory by using C#.

Open Microsoft Visual Studio 2013 => create C# windows application

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.DirectoryServices;

using System.IO;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        private const string IIS_PATH = @"IIS://localhost/W3SVC/1/ROOT";

 

        public Form1()

        {

            InitializeComponent();

        }       

 

        private void btnCreate_Click(object sender, EventArgs e)

        {

            CreateVirtualDirectoryIfNotExists();

        }

 

        private void CreateVirtualDirectoryIfNotExists()

        {

            string virtualDirectory = "CompanyApplication";

            if (!VirtualDirectoryExists(virtualDirectory))

            {

                List<string> listDefaultDocs = new List<string>();

                listDefaultDocs.Add("default.aspx");

                CreateVirtualDirectory("WebDirectory", virtualDirectory, true, true, false, false, true, false, listDefaultDocs.AsReadOnly());

            }

        }

 

        private static bool VirtualDirectoryExists(string virtualName)

        {

            DirectoryEntry root = new DirectoryEntry(IIS_PATH);

 

            foreach (DirectoryEntry childNode in root.Children)

                if (String.Compare(childNode.SchemaClassName, "IIsWebVirtualDir", true) == 0 && String.Compare(childNode.Name, virtualName, true) == 0)

                    return true;

 

            return false;

        }

 

        private void CreateVirtualDirectory(string path, string virtualName, bool accessRead, bool accessExecute, bool accessWrite,

                                                    bool accessScript, bool enableDefaultDoc, bool directoryBrowsing,

                                                    ReadOnlyCollection<string> defaultDocList)

        {

            DirectoryEntry root = new DirectoryEntry(IIS_PATH);

 

            if (!Directory.Exists(path))

                Directory.CreateDirectory(path);

 

            DirectoryEntry virtualDir = root.Children.Add(virtualName, "IIsWebVirtualDir");

            virtualDir.Properties["Path"].Value = path;

            virtualDir.Properties["AccessRead"][0] = accessRead;

            virtualDir.Properties["AccessExecute"][0] = accessExecute;

            virtualDir.Properties["AccessWrite"][0] = accessWrite;

            virtualDir.Properties["AccessScript"][0] = accessScript;

            virtualDir.Properties["AuthAnonymous"][0] = false;

            virtualDir.Properties["AuthBasic"][0] = false;

            virtualDir.Properties["AuthNTLM"][0] = true;

            virtualDir.Properties["EnableDefaultDoc"][0] = enableDefaultDoc;

            virtualDir.Properties["EnableDirBrowsing"][0] = directoryBrowsing;

            if (enableDefaultDoc)

            {

                if (defaultDocList != null && defaultDocList.Count > 0)

                {

                    StringBuilder defaultDocs = new StringBuilder();

 

                    for (int i = 0; i < defaultDocList.Count; i++)

                    {

                        if (i != 0)

                            defaultDocs.Append(",");

 

                        defaultDocs.Append(defaultDocList[i]);

                    }

                    virtualDir.Properties["DefaultDoc"][0] = defaultDocs.ToString();

                }

            }

            virtualDir.CommitChanges();

            virtualDir.Invoke("AppCreate2", new object[] { 2 });

        }

    }

}

 

As shown above we have three methods. CreateVirtualDirectoryIfNotExists() method checks whether virtual directory CompanyApplication exists or not by calling VirtualDirectoryExists() method. If virtual directory not exists it creates CompanyApplication virtual directory by calling CreateVirtualDirectory() method. In CreateVirtualDirectory() method we are setting the required permissions on the virtual directory.