ADSI 接口操作IIS

using  System;
using  System.Collections.Generic;
using  System.DirectoryServices;

namespace  Tool
{
    
public class IISHelper
    
{
        
-- 字段 --

        
-- 构造函数 --

        
-- 属性 --

        
-- 公开方法 --

        
private int GetNewSiteID()
        
{
            Random r 
= new Random((int)DateTime.Now.Ticks);
            
int newID = r.Next(_minSiteID, _maxSiteID);
            
while (!ChkIDUnique(newID)) //get unique id
            {
                newID 
= r.Next(_minSiteID, _maxSiteID);
            }

            
return newID;
        }

        
private int GetNewSiteIDBySort()
        
{
            
int newID = _minSiteID;
            
while (!ChkIDUnique(newID)) //get unique id
            {
                newID
++;
                
if (newID >= _maxSiteID)
                
{
                    
throw new Exception("id overflowed");
                }

            }

            
return newID;
        }

        
private bool ChkIDUnique(int siteID)
        
{
            
bool flag = true;
            DirectoryEntry root 
= new DirectoryEntry();
            root.Path 
= String.Format("IIS://{0}/W3SVC", _server);

            
foreach (DirectoryEntry son in root.Children)
            
{
                
if (son.SchemaClassName == "IIsWebServer")
                
{
                    
if (siteID == Int32.Parse(son.Name.Trim()))
                    
{
                        flag 
= false;
                    }

                }

            }

            
return flag;
        }

        
private bool ChkSiteUnique(List<string> domains)
        
{
            DirectoryEntry root 
= new DirectoryEntry();
            root.Path 
= String.Format("IIS://{0}/W3SVC", _server);
            
foreach (DirectoryEntry son in root.Children)
            
{
                
if (son.SchemaClassName == "IIsWebServer" && son.Properties["ServerBindings"].Value != null)
                
{
                    
for (int i = 0; i < domains.Count; i++)
                    
{
                        
for (int j = 0; j < son.Properties["ServerBindings"].Count; j++)
                        
{
                            
string serverBindingsStr;
                            
if (_server == "localhost")
                            
{
                                serverBindingsStr 
= ":" + _port.ToString() + ":" + domains[i];
                            }

                            
else
                            
{
                                serverBindingsStr 
= _server + ":" + _port.ToString() + ":" + domains[i];
                            }


                            
if (serverBindingsStr == son.Properties["ServerBindings"][j].ToString())
                            
{
                                
return false;
                            }

                        }

                    }

                }

            }

            
return true;
        }

    }

}

大概写了一下,用来做过两个WEB 应用的安装包,发现还可以完善
现在有一个问题就是当系统上有两个freamwork版本如1.1 2.0的时候,无法设置站点使其使用正确的版本,哪位兄台知道告诉我一下,谢谢了:)

下面是使用例子

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Configuration.Install;

namespace  InstallHelper
{
    [RunInstaller(
true)]
    
public partial class InstallHelper : Installer
    
{
        
public InstallHelper()
        
{
            InitializeComponent();
        }


        
public override void Install(System.Collections.IDictionary stateSaver)
        
{
            
base.Install(stateSaver);

            

            
string _serverComment = this.Context.Parameters["serverComment"];
            
string _server = this.Context.Parameters["server"];
            
string _port = this.Context.Parameters["port"];
            
string _domain = this.Context.Parameters["domain"];
            
string _localPath = this.Context.Parameters["targetdir"];

            Tool.IISHelper iis 
= new Tool.IISHelper();
            iis.Server 
= _server;
            List
<string> domains = new List<string>();
            domains.Add(_domain);
            iis.DoMain 
= domains;
            iis.Port 
= Int32.Parse(_port);
            iis.ServerComment 
= _serverComment;
            iis.LocalPath 
= _localPath;
            iis.CreateSite();

            stateSaver.Add(
"siteID", iis.SiteID);
        }


        
public override void Uninstall(System.Collections.IDictionary savedState)
        
{
            
base.Uninstall(savedState);
            
if (savedState.Count != 0)
            
{
                
if (savedState.Contains("siteID"))
                
{
                    
int siteID = Int32.Parse(savedState["siteID"].ToString());
                    Tool.IISHelper iis 
= new Tool.IISHelper();
                    iis.DeleteSiteById(siteID);
                }

            }


            
        }


        
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值