希望高手帮忙,如能解决小弟感激涕零,坚决给分,代码如下:
#region Connect 连接IIS服务器        public bool Connect()
        {
            if (iis == null)
                return false;
            try
            {
                iisServer = new DirectoryEntry("IIS://" + iis + "/W3SVC/1");
                target = iis;
                connection = new ConnectionOptions();
                scope = new ManagementScope(@"\\" + iis + @"\root\MicrosoftIISV2", connection);
                scope.Connect();
            }
            catch
            {
                return false;            }
            return IsConnected();
        }
        public bool IsConnected()
        {
            if (target == null || connection == null || scope == null) return false;
            return scope.IsConnected;
        }
        #endregion        #region IsWebSiteExists 判断网站是否已经存在
        public bool IsWebSiteExists(string serverID)
        {
            try
            {
                string siteName = "W3SVC/" + serverID;
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);
                ManagementObjectCollection webSites = searcher.Get();
                foreach (ManagementObject webSite in webSites)
                {                    if ((string)webSite.Properties["Name"].Value == siteName)
                        return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        #region GetNextOpenID 获得一个新的ServerID        private int GetNextOpenID()
        {
            DirectoryEntry iisComputer = new DirectoryEntry("IIS://localhost/w3svc");
            int nextID = 0;
            foreach (DirectoryEntry iisWebServer in iisComputer.Children)
            {
                string sname = iisWebServer.Name;
                try
                {
                    int name = int.Parse(sname);
                    if (name > nextID)
                    {
                        nextID = name;
                    }
                }                catch
                {                }            }            return ++nextID;        }        #endregion        #region CreateWebsite 添加网站        public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)
        {
            try
            {                ManagementObject oW3SVC = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
                if (IsWebSiteExists(serverID))
                {
                    return "网站已经存在...";                }
                ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
                ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];
                serverBinding[0] = CreateServerBinding(HostName, IP, Port);
                inputParameters["ServerComment"] = serverComment;
                inputParameters["ServerBindings"] = serverBinding;
                inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;
                inputParameters["ServerId"] = serverID;                                ManagementBaseObject outParameter = null;
                outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
                // 启动网站
                string serverName = "W3SVC/" + serverID;
                ManagementObject webSite = new ManagementObject(scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
                webSite.InvokeMethod("Start", null);
                return (string)outParameter.Properties["ReturnValue"].Value;            }
            catch (Exception ex)
            {
                return ex.Message;
            }        }
        public ManagementObject CreateServerBinding(string HostName, string IP, string Port)
        {
            try
            {                ManagementClass classBinding = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
                ManagementObject serverBinding = classBinding.CreateInstance();
                serverBinding.Properties["Hostname"].Value = HostName;
                serverBinding.Properties["IP"].Value = IP;
                serverBinding.Properties["Port"].Value = Port;
                serverBinding.Put();
                return serverBinding;
            }
            catch
            {
                return null;
            }        }        #endregion        public string getIP() //获取IP
        {
            string HostName = System.Net.Dns.GetHostName(); //得到主机名
            IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //得到主机IP
            string strIPAddr = IpEntry.AddressList[0].ToString();
            return (strIPAddr);
        }
        #region
        //创建桌面快捷方式及图标,借助.url生成网络快捷方式  
        private void createShortCut()
        {
            //获取软件安装的物理目录,形如:D:\Inetpub\wwwroot\Endy_Web\InstallClass.dll     
            string fname = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //也可以获取软件安装的物理目录,和上面获取的结果一样,形如:D:\Inetpub\wwwroot\Endy_Web\InstallClass.dll
            //string fname2 = Context.Parameters["assemblypath"];
            string[] sz = fname.Split(new char[] { '\\' });
            StreamWriter sw = new StreamWriter(File.Open(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                + "\\shortCut.url", FileMode.Create, FileAccess.Write));
            sw.WriteLine("[InternetShortcut]");       
            sw.WriteLine("URL=http://" + getIP() + "/Default.aspx");            //MessageBox.Show("IconFile=" + fname.Substring(0, fname.LastIndexOf("\\")) + "\\ICO\\logo.ico");            sw.WriteLine("IconFile=" + fname.Substring(0, fname.LastIndexOf("\\")) + "\\ICO\\logo.ico");
            sw.WriteLine("IconIndex=0");
            sw.Flush();
            sw.Close();        }
        #endregion
       
        //整个方法写完后如下:
        #region Install 安装
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            dir = this.Context.Parameters["targetdir"];
            DBName = this.Context.Parameters["dbname"].ToString();
            ServerName = this.Context.Parameters["server"].ToString();
            AdminName = this.Context.Parameters["user"].ToString();
            AdminPwd = this.Context.Parameters["pwd"].ToString();
            iis = this.Context.Parameters["iis"].ToString();
            port = this.Context.Parameters["port"].ToString();
            websitenName = this.Context.Parameters["WEBSITENNAME"].ToString();            
            //写入获取的安装程序中的变量,此段代码为调试用可以不添加
            this.sqlConn = new System.Data.SqlClient.SqlConnection();
            this.sqlConn.ConnectionString = "User ID=" + AdminName + ";Data Source=" + ServerName
                + ";Password=" + AdminPwd + ";Persist Security Info=True;Integrated Security=false";            // 执行SQL 安装数据库 可选择时恢复或者时直接创建
            if (!CreateDBAndTable(DBName))
            {
                throw new ApplicationException("创建数据库时出现严重错误!");
            }
            //从备份数据库文件恢复数据库
            if (!RestoreDB(DBName))
            {
                throw new ApplicationException("恢复数据库时出现严重错误!");
            }            // 添加网站
            Connect();
            string serverID = GetNextOpenID().ToString();
            string serverComment = websitenName;            //string defaultVrootPath = this.Context.Parameters["targetdir"];            if (dir.EndsWith(@"\"))
            {                dir = dir.Substring(0, dir.Length - 1);            }            string HostName = getIP();
            string IP = getIP();
            string Port = port;
            string sReturn = CreateWebSite(serverID, serverComment, dir, HostName, IP, Port);            MessageBox.Show(sReturn);            // 修改web.config
            if (!WriteWebConfig())
            {
                throw new ApplicationException("设置数据库连接字符串时出现错误");
            }
            //创建快捷方式
            createShortCut();
            
            // 写注册表
            //WriteRegistryKey();
        }