Create Virtualdirectory in existing website in iis

  Create virtual directory in already existing website in iis 7.0 using asp.net,C#

try
    {
        CreateVirtualDir("localhost", "websiteName" ,"MyVirtualDirectory123", @"E:\testclub");
    }
    catch (Exception ex)
    {
        lblMessage.Text = string.Concat("An error occurred: ", ex.Message);
    }

 private void CreateVirtualDir(string serverName, string website, string appName, string path)
    {
        DirectoryEntry IISSchema = new DirectoryEntry(string.Concat("IIS://", serverName, "/Schema/AppIsolated"));
        bool canCreate = IISSchema.Properties["Syntax"].Value.ToString().ToUpper() != "BOOLEAN";
        IISSchema.Dispose();

        //get the identifier for the site we want
        int identifier = 0;
        DirectoryEntry root = new DirectoryEntry(string.Concat("IIS://", serverName, "/W3SVC"));
        foreach (DirectoryEntry de in root.Children)
        {
            if (de.SchemaClassName.ToUpper().Equals("IISWEBSERVER") &&
                de.Invoke("Get", "ServerComment").ToString().ToUpper().Equals(website.ToUpper()))
            {
                identifier = Convert.ToInt32(de.Name);
                break;
            }
        }

        if (canCreate && identifier > 0)
        {
            bool pathCreated = false;
            try
            {
                DirectoryEntry iisAdmin = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/{1}/Root", serverName, identifier));

                //make sure folder exists
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                    pathCreated = true;
                }

                //If the virtual directory already exists then delete it
                foreach (DirectoryEntry vd in iisAdmin.Children)
                {
                    if (vd.Name.Equals(appName))
                    {
                        iisAdmin.Invoke("Delete", new string[] { vd.SchemaClassName, appName });
                        iisAdmin.CommitChanges();
                        break;
                    }
                }

                //Create and setup new virtual directory
                DirectoryEntry vDir = iisAdmin.Children.Add(appName, "IIsWebVirtualDir");
                vDir.Properties["Path"][0] = path;
                vDir.Properties["AppFriendlyName"][0] = appName;
                vDir.Properties["EnableDirBrowsing"][0] = false;
                vDir.Properties["AccessRead"][0] = true;
                vDir.Properties["AccessExecute"][0] = true;
                vDir.Properties["AccessWrite"][0] = false;
                vDir.Properties["AccessScript"][0] = true;
                vDir.Properties["AuthNTLM"][0] = true;
                vDir.Properties["EnableDefaultDoc"][0] = true;
                vDir.Properties["DefaultDoc"][0] = "default.htm,default.aspx,default.asp";
                vDir.Properties["AspEnableParentPaths"][0] = true;
                vDir.CommitChanges();

                //the following are acceptable params
                //INPROC = 0
                //OUTPROC = 1
                //POOLED = 2
                vDir.Invoke("AppCreate", 1);
            }
            catch (Exception ex)
            {
                if (pathCreated)
                {
                    System.IO.Directory.Delete(path);
                }

                throw ex;
            }
        }
        else
        {
            throw new ApplicationException("Failed to create Virtual Directory");
        }
    }

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender