这里提供一下创建虚拟目录的大体方法,虚拟目录是通过Root节点去创建的:
创建虚拟目录
public bool CreateWebVirtualDir(string virtualName, string virtualPath, string siteID, out string msg)
{
try
{
msg = "";
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID + "/root";//这里是一个Root节点的DirectoryEntry
DirectoryEntry siteVDir = tempEntry.Children.Add(virtualName, "IISWebVirtualDir");
siteVDir.Invoke("AppCreate", true);
siteVDir.Properties["Path"][0] = virtualPath;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["AppFriendlyName"][0] = virtualName;
siteVDir.Properties["AuthFlags"][0] = 0;
siteVDir.Properties["AccessScript"][0] = true;
siteVDir.Properties["AccessSource"][0] = true;
siteVDir.Properties["AuthNTLM"][0] = true;//集成win身份验证
siteVDir.Properties["AuthAnonymous"][0] = true;//集成win身份验证
siteVDir.Properties["DefaultDoc"][0] = WebSiteDefaultDoc;
siteVDir.Invoke("AppCreate2", new object[1] { 2 });
tempEntry.CommitChanges();
siteVDir.CommitChanges();
return true;
}
catch (Exception err)
{
msg = err.Message;
}
return false;
}
{
try
{
msg = "";
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID + "/root";//这里是一个Root节点的DirectoryEntry
DirectoryEntry siteVDir = tempEntry.Children.Add(virtualName, "IISWebVirtualDir");
siteVDir.Invoke("AppCreate", true);
siteVDir.Properties["Path"][0] = virtualPath;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["AppFriendlyName"][0] = virtualName;
siteVDir.Properties["AuthFlags"][0] = 0;
siteVDir.Properties["AccessScript"][0] = true;
siteVDir.Properties["AccessSource"][0] = true;
siteVDir.Properties["AuthNTLM"][0] = true;//集成win身份验证
siteVDir.Properties["AuthAnonymous"][0] = true;//集成win身份验证
siteVDir.Properties["DefaultDoc"][0] = WebSiteDefaultDoc;
siteVDir.Invoke("AppCreate2", new object[1] { 2 });
tempEntry.CommitChanges();
siteVDir.CommitChanges();
return true;
}
catch (Exception err)
{
msg = err.Message;
}
return false;
}
下面再给出一些常用的方法:
网站同名检测:
确认网站是否相同
private bool CheckSiteExists(out string msg)
{
msg = "";
foreach (DirectoryEntry child in iisEntry.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerComment"].Value != null)
{
if (child.Properties["ServerComment"].Value.ToString().ToLower() == WebSiteName.ToLower())
{
msg = "站点名称已存在!";
return true;
}
}
}
}
return false;
}
private bool CheckSiteExists(out string msg)
{
msg = "";
foreach (DirectoryEntry child in iisEntry.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerComment"].Value != null)
{
if (child.Properties["ServerComment"].Value.ToString().ToLower() == WebSiteName.ToLower())
{
msg = "站点名称已存在!";
return true;
}
}
}
}
return false;
}
删除一个站点:
站点删除
public bool DeleteWebSiteByID(string siteID)
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
iisEntry.Children.Remove(tempEntry);
iisEntry.CommitChanges();
return true;
}
catch
{
}
return false;
}
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
iisEntry.Children.Remove(tempEntry);
iisEntry.CommitChanges();
return true;
}
catch
{
}
return false;
}
Start和Stop网站:
站点停止和启动
public bool StartWebSite(string siteID)
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
tempEntry.Invoke("Start", new object[] { });
return true;
}
catch
{ }
return false;
}
public bool StopWebSite(string siteID)
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
tempEntry.Invoke("Stop", new object[] { });
return true;
}
catch { }
return false;
}
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
tempEntry.Invoke("Start", new object[] { });
return true;
}
catch
{ }
return false;
}
public bool StopWebSite(string siteID)
{
try
{
siteID = (string.IsNullOrEmpty(siteID) ? WebSiteID : siteID);
tempEntry.Path = IISEntryPath + "/" + siteID;
tempEntry.Invoke("Stop", new object[] { });
return true;
}
catch { }
return false;
}
打完,收工!