这里提供一下创建虚拟目录的大体方法,虚拟目录是通过Root节点去创建的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
下面再给出一些常用的方法:
网站同名检测:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
删除一个站点:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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网站:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
打完,收工!
版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。 |
个人微信公众号![]() |
Donation(扫码支持作者):支付宝:![]() |
Donation(扫码支持作者):微信:![]() |
![]() |