![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/// <summary>
/// 获取新网站ID
/// </summary>
/// <returns></returns>
private string GetNewWebSiteID()
{
int iWebSiteCount = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DirectoryEntry childEntry in siteEntry.Children)
{
if (childEntry.SchemaClassName == "IIsWebServer") iWebSiteCount++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (iWebSiteCount + 1).ToString();
}
/// <summary>
/// 获取新站点端口,默认为当前最大端口号加一
/// </summary>
/// <returns></returns>
private string GetNewSitePort()
{
int iDefault = 8000;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DirectoryEntry childEntry in siteEntry.Children)
{
if (childEntry.SchemaClassName == "IIsWebServer")
{
if (childEntry.Properties["ServerBindings"].Value != null)
{
string strSettings = childEntry.Properties["ServerBindings"].Value.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int iSettingPort = int.Parse(strSettings.Substring(strSettings.IndexOf(':') + 1, (strSettings.LastIndexOf(':') - strSettings.IndexOf(':') - 1)));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
iDefault = iSettingPort > iDefault ? iSettingPort : iDefault;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (iDefault + 1).ToString();
}
/// <summary>
/// 判断网站是否已经存在
/// </summary>
/// <param name="strSiteName"></param>
/// <returns></returns>
private bool IsExist(string strSiteName)
{
bool blExist = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DirectoryEntry childEntry in siteEntry.Children)
{
if (childEntry.SchemaClassName == "IIsWebServer")
{
if (childEntry.Properties["ServerComment"].Value != null)
{
if (childEntry.Properties["ServerComment"].Value.ToString() == strSiteName) blExist = true;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return blExist;
}
/// <summary>
/// 创建网站
/// </summary>
/// <param name="strPort"></param>
/// <param name="strSiteName"></param>
/// <param name="strFilePath"></param>
/// <returns></returns>
private DirectoryEntry CreateNewSite(string strSiteName, string strFilePath)
{
try
{
strSitePort = GetNewSitePort();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DirectoryEntry deyRoot = new DirectoryEntry("IIS://localhost/w3svc");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DirectoryEntry siteEntry = deyRoot.Children.Add(GetNewWebSiteID(), "IIsWebServer");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
siteEntry.Properties["ServerComment"].Value = strSiteName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
siteEntry.Properties["ServerBindings"].Value = String.Format("{0}:{1}:{2}", "", strSitePort, "");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
siteEntry.Properties["ServerAutoStart"].Value = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加虚拟目录,网站本身没有路径,默认的路径即名称为Root的虚拟目录
DirectoryEntry rootEntry = siteEntry.Children.Add("Root", "IIsWebVirtualDir");
rootEntry.Properties["Path"].Value = strFilePath; //文件夹路径
rootEntry.Properties["AccessRead"][0] = true; //读取权限
rootEntry.Properties["AccessExecute"][0] = false; //执行(如ISAPI应用程序或CGI)
rootEntry.Properties["AccessWrite"][0] = true; //写入
rootEntry.Properties["AccessScript"][0] = true; //执行脚本(如ASP)
rootEntry.Properties["EnableDirBrowsing"][0] = true; //浏览
rootEntry.Properties["DefaultDoc"][0] = "UserLogin.aspx,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割
rootEntry.CommitChanges(); siteEntry.CommitChanges(); return rootEntry;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + "站点创建失败!"); return null;
}
}
/// <summary>
/// 创建虚拟目录
/// </summary>
/// <param name="strDirName">虚拟目录名称</param>
/// <param name="strDirPath">虚拟目录路径</param>
/// <param name="siteEntry">要创建虚拟目录的原站点</param>
/// <returns></returns>
private bool CreateVirtualDirectory(string strDirName, string strDirPath, DirectoryEntry rootEntry)
{
try
{
DirectoryEntry childEntry = rootEntry.Children.Add(strDirName, "IIsWebVirtualDir");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
childEntry.Invoke("AppCreate", true);
childEntry.Properties["Path"].Value = strDirPath;// @"D:\公司项目\盘县联网\SafetyProduce\SafetyNet\"; //文件夹路径
childEntry.Properties["AppFriendlyName"][0] = strDirName;//应用程序名称
childEntry.Properties["AccessRead"][0] = true; //读取权限
childEntry.Properties["AccessExecute"][0] = false; //执行(如ISAPI应用程序或CGI)
childEntry.Properties["AccessWrite"][0] = true; //写入
childEntry.Properties["AccessScript"][0] = true; //执行脚本(如ASP)
childEntry.Properties["EnableDirBrowsing"][0] = true; //浏览
childEntry.Properties["DefaultDoc"][0] = "UserLogin.aspx,Default.aspx"; ////设置默认文档,多值情况下中间用逗号分割
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
childEntry.CommitChanges(); rootEntry.CommitChanges(); return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + "虚拟目录" + strDirName + "创建失败!"); return false;
}
}
以下为调用代码
//创建站点
DirectoryEntry rootEntry = CreateNewSite("天科行业主管平台", strDirectoryPath + "WebPage");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if (rootEntry != null)
{
//创建虚拟目录
CreateVirtualDirectory("SafetyNet", strDirectoryPath + "SafetyNet", rootEntry);
CreateVirtualDirectory("SafetySupervise", strDirectoryPath + "SafetySupervise", rootEntry);
}
posted @
2013-04-24 11:27
fang_beny
阅读(
172)
评论()
编辑
收藏
举报