The following code will help to create Website under IIS
public static void createWebSite(string strSiteID, string strSiteName, string strWebAppPath)
{
CreateSite(strSiteID, strSiteName, strWebAppPath);
SetSingleProperty(strMetaBasePath + "/" + strSiteID, "ServerBindings", ":8080:");
CreateVDir(strMetaBasePath + "/1/Root", strSiteName, strWebAppPath);
}
static void CreateSite(string siteID, string siteName, string physicalPath)
{
try
{
DirectoryEntry service = new DirectoryEntry(strMetaBasePath);
string className = service.SchemaClassName.ToString();
if (className.EndsWith("Service"))
{
DirectoryEntries sites = service.Children;
DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
newSite.Properties["ServerComment"][0] = siteName;
newSite.CommitChanges();
DirectoryEntry newRoot;
newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
newRoot.Properties["Path"][0] = physicalPath;
newRoot.Properties["AccessScript"][0] = true;
newRoot.CommitChanges();
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeline(ex.Message);
throw ex;
}
}
static void SetSingleProperty(string metabasePath, string propertyName, object newValue)
{
try
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyValueCollection propValues = path.Properties[propertyName];
string oldType = propValues.Value.GetType().ToString();
string newType = newValue.GetType().ToString();
Console.WriteLine(" Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType);
if (newType == oldType)
{
path.Properties[propertyName][0] = newValue;
path.CommitChanges();
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeliner(ex.Message);
throw ex;
}
}
static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
try
{
DirectoryEntry site = new DirectoryEntry(metabasePath);
string className = site.SchemaClassName.ToString();
if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
{
DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
newVDir.Properties["Path"][0] = physicalPath;
newVDir.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
newVDir.Properties["AppFriendlyName"][0] = vDirName;
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));
newVDir.CommitChanges();
Object nativeObject = newVDir.NativeObject;
nativeObject.GetType().InvokeMember("AppCreate3", BindingFlags.InvokeMethod, null, nativeObject, new object[] { 2, "DefaultAppPool", false }); // 2 means to use an Application Pool.
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeline(ex.Message);
throw ex;
}
}
{
CreateSite(strSiteID, strSiteName, strWebAppPath);
SetSingleProperty(strMetaBasePath + "/" + strSiteID, "ServerBindings", ":8080:");
CreateVDir(strMetaBasePath + "/1/Root", strSiteName, strWebAppPath);
}
static void CreateSite(string siteID, string siteName, string physicalPath)
{
try
{
DirectoryEntry service = new DirectoryEntry(strMetaBasePath);
string className = service.SchemaClassName.ToString();
if (className.EndsWith("Service"))
{
DirectoryEntries sites = service.Children;
DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
newSite.Properties["ServerComment"][0] = siteName;
newSite.CommitChanges();
DirectoryEntry newRoot;
newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
newRoot.Properties["Path"][0] = physicalPath;
newRoot.Properties["AccessScript"][0] = true;
newRoot.CommitChanges();
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeline(ex.Message);
throw ex;
}
}
static void SetSingleProperty(string metabasePath, string propertyName, object newValue)
{
try
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyValueCollection propValues = path.Properties[propertyName];
string oldType = propValues.Value.GetType().ToString();
string newType = newValue.GetType().ToString();
Console.WriteLine(" Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType);
if (newType == oldType)
{
path.Properties[propertyName][0] = newValue;
path.CommitChanges();
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeliner(ex.Message);
throw ex;
}
}
static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
try
{
DirectoryEntry site = new DirectoryEntry(metabasePath);
string className = site.SchemaClassName.ToString();
if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
{
DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
newVDir.Properties["Path"][0] = physicalPath;
newVDir.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
newVDir.Properties["AppFriendlyName"][0] = vDirName;
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));
newVDir.CommitChanges();
Object nativeObject = newVDir.NativeObject;
nativeObject.GetType().InvokeMember("AppCreate3", BindingFlags.InvokeMethod, null, nativeObject, new object[] { 2, "DefaultAppPool", false }); // 2 means to use an Application Pool.
}
else
{
}
}
catch (Exception ex)
{
Debug.Writeline(ex.Message);
throw ex;
}
}
No comments:
Post a Comment