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);
                }

            }


            
        }


        
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章