Creating Setup and Deployment Projects in VS2005.NET

     最近我们团队完成了项目开发,要我做一个安装程序.以前从来没有进行过,哈哈^先花了半天学习,最后做了一个安装程序.但是出现在了一个问题,希望能得到高手的指教:
在我安装在最后的关头出现了这样的错误:未将对象设置引用到对象的实例!
Creating Setup and Deployment Projects in VS2005.NET

安装程序又无法进行调试,找了好多次还是找不到哪里出现了问题.后面附上了安装类库的代码:
我的解决方案:
Creating Setup and Deployment Projects in VS2005.NET

UI设计(1):
Creating Setup and Deployment Projects in VS2005.NET

UI设计(2):
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET

类库的代码有点长:
Creating Setup and Deployment Projects in VS2005.NETusing System;
Creating Setup and Deployment Projects in VS2005.NET
using System.IO;
Creating Setup and Deployment Projects in VS2005.NET
using System.Data;
Creating Setup and Deployment Projects in VS2005.NET
using System.Configuration.Install;
Creating Setup and Deployment Projects in VS2005.NET
using System.DirectoryServices;
Creating Setup and Deployment Projects in VS2005.NET
using System.Reflection;
Creating Setup and Deployment Projects in VS2005.NET
using System.Data.SqlClient;
Creating Setup and Deployment Projects in VS2005.NET
using System.Collections.Specialized;
Creating Setup and Deployment Projects in VS2005.NET
using System.Xml;
Creating Setup and Deployment Projects in VS2005.NET
using Microsoft.Win32;
Creating Setup and Deployment Projects in VS2005.NET
using System.ComponentModel;
Creating Setup and Deployment Projects in VS2005.NET
using System.Management;
Creating Setup and Deployment Projects in VS2005.NET
using System.Collections;
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET
namespace EightySharplibary
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET    [RunInstaller(
true)]
Creating Setup and Deployment Projects in VS2005.NET    
public partial class EightySharpInstaller : Installer
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET    
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET        
private SqlConnection sqlConn = null;
Creating Setup and Deployment Projects in VS2005.NET        
private SqlCommand Command;
Creating Setup and Deployment Projects in VS2005.NET        
private static string DBName;
Creating Setup and Deployment Projects in VS2005.NET        
private string AnotherDB;
Creating Setup and Deployment Projects in VS2005.NET        
private string ServerName;
Creating Setup and Deployment Projects in VS2005.NET        
private string AdminName;
Creating Setup and Deployment Projects in VS2005.NET        
private string AdminPwd;
Creating Setup and Deployment Projects in VS2005.NET        
private string iis;
Creating Setup and Deployment Projects in VS2005.NET        
private string port;
Creating Setup and Deployment Projects in VS2005.NET        
private string dir;
Creating Setup and Deployment Projects in VS2005.NET        
private string _target;
Creating Setup and Deployment Projects in VS2005.NET        
private DirectoryEntry _iisServer;
Creating Setup and Deployment Projects in VS2005.NET        
private ManagementScope _scope;
Creating Setup and Deployment Projects in VS2005.NET        
private ConnectionOptions _connection;
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET        
public EightySharpInstaller()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            InitializeComponent();
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
//connect the db.
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        ConnectionDatabase#region ConnectionDatabase
Creating Setup and Deployment Projects in VS2005.NET        
private bool ConnectDatabase()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
if (Command.Connection.State != ConnectionState.Open)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    Command.Connection.Open();
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
catch 
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    
return false;
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return true;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Get the db file 
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        GetSQLFiles#region GetSQLFiles 
Creating Setup and Deployment Projects in VS2005.NET        
private string GetSql(string name)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                Assembly ass 
= Assembly.GetExecutingAssembly();
Creating Setup and Deployment Projects in VS2005.NET                Stream strm 
= ass.GetManifestResourceStream(ass.GetName().Name + "." + name);
Creating Setup and Deployment Projects in VS2005.NET                StreamReader reader 
= new StreamReader(strm);
Creating Setup and Deployment Projects in VS2005.NET                
return reader.ReadToEnd();
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch (Exception exp)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new ApplicationException(exp.Message);
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//ExecuteSQL statement
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Execute sql statement.#region Execute sql statement.
Creating Setup and Deployment Projects in VS2005.NET        
private void ExecuteSql(string DataBaseName, string sqlstring)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            Command 
= new SqlCommand(sqlstring, sqlConn);
Creating Setup and Deployment Projects in VS2005.NET            
if (ConnectDatabase())
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    Command.Connection.ChangeDatabase(DataBaseName);
Creating Setup and Deployment Projects in VS2005.NET                    Command.ExecuteNonQuery();
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
catch (Exception exp)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    
throw new Exception(exp.Message);
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
finally
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    Command.Connection.Close();
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Create database and attach the files.
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Create DB and attach the file.#region Create DB and attach the file.
Creating Setup and Deployment Projects in VS2005.NET        
protected bool CreateDBAndTable(string DBName)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            AnotherDB 
= DBName + "Profile";
Creating Setup and Deployment Projects in VS2005.NET            
bool result = false;
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + DBName + "') DROP DATABASE " + DBName);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""CREATE DATABASE " + DBName);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(DBName, GetSql(
"HBSTPSQL.txt"));
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + AnotherDB + "') DROP DATABASE " + AnotherDB);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""CREATE DATABASE " + AnotherDB);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(AnotherDB, GetSql(
"ASPNETDBSQL.txt"));
Creating Setup and Deployment Projects in VS2005.NET                result 
= true;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch 
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                File.Delete(dir 
+ @"HBSTPSQl.txt");
Creating Setup and Deployment Projects in VS2005.NET                File.Delete(dir 
+ @"ASPNETDBSQL.txt");
Creating Setup and Deployment Projects in VS2005.NET                result 
= false;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return result;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
RestoreDB Backup the data from the files .bak.#region RestoreDB Backup the data from the files .bak.
Creating Setup and Deployment Projects in VS2005.NET        
protected bool RestoreDB(string DBName)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            AnotherDB 
= DBName + "Profile";
Creating Setup and Deployment Projects in VS2005.NET            dir 
= this.Context.Parameters["targetdir"];
Creating Setup and Deployment Projects in VS2005.NET            
bool Restult = false;
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET            
string MSQL1 = "RESTORE DATABASE " + DBName +
Creating Setup and Deployment Projects in VS2005.NET                
" FROM DISK = '" + dir + @"HBSTPDB.bak' " +
Creating Setup and Deployment Projects in VS2005.NET                
" WITH MOVE 'HBSTPDB' TO '" + @"C:"Program Files"Microsoft SQL Server"MSSQL.1"MSSQL"Data"" + DBName + ".mdf', " +
Creating Setup and Deployment Projects in VS2005.NET                
" MOVE 'HBSTPDB_log' TO '" + @"C:"Program Files"Microsoft SQL Server"MSSQL.1"MSSQL"Data"" + DBName + ".ldf' ";
Creating Setup and Deployment Projects in VS2005.NET            
string MSQL2 = "RESTORE DATABASE " + AnotherDB +
Creating Setup and Deployment Projects in VS2005.NET               
" FROM DISK = '" + dir + @"ASPNETDB.bak' " +
Creating Setup and Deployment Projects in VS2005.NET               
" WITH MOVE 'ADPNETDB' TO '" + @"C:"Program Files"Microsoft SQL Server"MSSQL.1"MSSQL"Data"" + AnotherDB + ".mdf', " +
Creating Setup and Deployment Projects in VS2005.NET               
" MOVE 'aspnetdb_log' TO '" + @"C:"Program Files"Microsoft SQL Server"MSSQL.1"MSSQL"Data"" + AnotherDB + ".ldf' ";
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + DBName + "') DROP DATABASE " + DBName);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master", MSQL1);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master""USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + AnotherDB + "') DROP DATABASE " + AnotherDB);
Creating Setup and Deployment Projects in VS2005.NET                ExecuteSql(
"master", MSQL2);
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET                Restult 
= true;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
finally
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
// delete the backup files
Creating Setup and Deployment Projects in VS2005.NET
                try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    File.Delete(dir 
+ @"HBSTPDB.bak");
Creating Setup and Deployment Projects in VS2005.NET                    File.Delete(dir 
+ @"ASPNETDB.bak");
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
catch
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return Restult;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Modify the web.config 
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Modify teh web.config connectionstring.#region   Modify teh web.config connectionstring.
Creating Setup and Deployment Projects in VS2005.NET        
private bool WriteWebConfig()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            FileInfo fileInfo 
= new FileInfo(this.Context.Parameters["targetdir"+ "/web.config");
Creating Setup and Deployment Projects in VS2005.NET            
if (!fileInfo.Exists)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new InstallException("Missing config file:" + this.Context.Parameters["targetdir"+ "/web.config");
Creating Setup and Deployment Projects in VS2005.NET            }
       
Creating Setup and Deployment Projects in VS2005.NET        XmlDocument xmldoc 
= new XmlDocument();
Creating Setup and Deployment Projects in VS2005.NET        xmldoc.Load(fileInfo.FullName);
Creating Setup and Deployment Projects in VS2005.NET        
bool connection1 = false;
Creating Setup and Deployment Projects in VS2005.NET        
bool connection2 = false;
Creating Setup and Deployment Projects in VS2005.NET        
bool foundIt = connection1 && connection2;
Creating Setup and Deployment Projects in VS2005.NET        
foreach(XmlNode node in xmldoc["configuration"]["connectionStrings"])
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
if (node.Name == "add")
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
if (node.Attributes.GetNamedItem("name").Value == "HBSTPConnection")
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    node.Attributes.GetNamedItem(
"connectionString").Value = String.Format("Persist Security Info=False; Data Source={0}; Initial Catalog={1}; User ID={2}; Password={3}; Packet Size=4096; Pooling=true; Max Pool Size=100; Min Pool Size=1", ServerName, DBName, AdminName, AdminPwd);
Creating Setup and Deployment Projects in VS2005.NET                    connection1 
= true;
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
if (node.Attributes.GetNamedItem("name").Value == "ConnectionString")
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    node.Attributes.GetNamedItem(
"connectionString").Value = String.Format("Persist Security Info=False; Data Source={0};Initial Catalog={1}; User ID={2}; Password={3}; Packet Size=4096; Pooling=true; Max Pool Size=100; Min Pool Size=1", ServerName, AnotherDB, AdminName, AdminPwd);
Creating Setup and Deployment Projects in VS2005.NET                    connection2 
= true;
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
if (!foundIt)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
throw new InstallException("Error when writing the config file: web.config");
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        xmldoc.Save(fileInfo.FullName);
Creating Setup and Deployment Projects in VS2005.NET        
return foundIt;      
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Write the registrykey
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Write RegistryKey.#region   Write RegistryKey.
Creating Setup and Deployment Projects in VS2005.NET        
private void WriteRegistryKey()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
//Write registrykey
Creating Setup and Deployment Projects in VS2005.NET
            RegistryKey rote = Registry.LocalMachine;
Creating Setup and Deployment Projects in VS2005.NET            RegistryKey EightySharp 
= rote.OpenSubKey("SOFTWARE"true);
Creating Setup and Deployment Projects in VS2005.NET            RegistryKey thirdSub 
= EightySharp.CreateSubKey("EightySharp");
Creating Setup and Deployment Projects in VS2005.NET            thirdSub.SetValue(
"FilePath""80Sharp");
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Connect the iis server
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Connect iis server#region Connect iis server
Creating Setup and Deployment Projects in VS2005.NET        
public bool Connect()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
if (iis == null)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
return false;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                _iisServer 
= new DirectoryEntry("IIS://" + iis + "/W3SVC/1");
Creating Setup and Deployment Projects in VS2005.NET                _target 
= iis;
Creating Setup and Deployment Projects in VS2005.NET                _connection 
= new ConnectionOptions();
Creating Setup and Deployment Projects in VS2005.NET                _scope 
= new ManagementScope(@"""" + iis + @""root"MicrosoftIISV2", _connection);
Creating Setup and Deployment Projects in VS2005.NET                _scope.Connect();
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
return false;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return IsConnected();
Creating Setup and Deployment Projects in VS2005.NET         }

Creating Setup and Deployment Projects in VS2005.NET      
//
Creating Setup and Deployment Projects in VS2005.NET
        public bool IsConnected()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
if (_target == null || _connection == null || _scope == null)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
return false;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return _scope.IsConnected;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Is Web site Exists
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Is web site exists#region  Is web site exists
Creating Setup and Deployment Projects in VS2005.NET        
public bool IsWebSiteExists(string serverID)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
bool result = false;
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
string siteName = "W3SVC/" + serverID;
Creating Setup and Deployment Projects in VS2005.NET                ManagementObjectSearcher searcher 
= new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);
Creating Setup and Deployment Projects in VS2005.NET                ManagementObjectCollection webSites 
= searcher.Get();
Creating Setup and Deployment Projects in VS2005.NET                
foreach (ManagementObject webSite in webSites)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    
if ((string)webSite.Properties["Name"].Value == siteName)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                    
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                        result 
= true ;
Creating Setup and Deployment Projects in VS2005.NET                    }

Creating Setup and Deployment Projects in VS2005.NET                    
return false;
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                result 
= false;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return result;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET        
//get next serverid
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Get next openID#region Get next openID
Creating Setup and Deployment Projects in VS2005.NET        
private int GetNextOpenID()
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            DirectoryEntry iisComputer 
= new DirectoryEntry("IIS://localhost/w3svc");
Creating Setup and Deployment Projects in VS2005.NET            
int nextID = 0;
Creating Setup and Deployment Projects in VS2005.NET            
foreach (DirectoryEntry iisWebServer in iisComputer.Children)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
string sname = iisWebServer.Name;
Creating Setup and Deployment Projects in VS2005.NET                
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    
int name = int.Parse(sname);
Creating Setup and Deployment Projects in VS2005.NET                    
if (name > nextID)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                    
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                        nextID 
= name;
Creating Setup and Deployment Projects in VS2005.NET                    }

Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                
catch
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
return ++nextID;
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//create web site
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Create Web site#region Create Web site
Creating Setup and Deployment Projects in VS2005.NET        
public string CreateWebSite(string serverID, string servercomment, string defaultVrootPath, string HostName, string IP, string Port)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                ManagementObject oW3SVC 
= new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
Creating Setup and Deployment Projects in VS2005.NET                
if (IsWebSiteExists(serverID))
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET                
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                    
return "Site Already existsCreating Setup and Deployment Projects in VS2005.NET";
Creating Setup and Deployment Projects in VS2005.NET                }

Creating Setup and Deployment Projects in VS2005.NET                ManagementBaseObject inputParameters 
= oW3SVC.GetMethodParameters("CreateNewSite");
Creating Setup and Deployment Projects in VS2005.NET                ManagementBaseObject[] serverBinding 
= new ManagementBaseObject[1];
Creating Setup and Deployment Projects in VS2005.NET                serverBinding[
0= CreateServerBinding(HostName, IP, Port);
Creating Setup and Deployment Projects in VS2005.NET                inputParameters[
"ServerComment"= servercomment;
Creating Setup and Deployment Projects in VS2005.NET                inputParameters[
"ServerBindings"= serverBinding;
Creating Setup and Deployment Projects in VS2005.NET                inputParameters[
"PathOfRootVirtualDir"= defaultVrootPath;
Creating Setup and Deployment Projects in VS2005.NET                inputParameters[
"ServerId"= serverID;
Creating Setup and Deployment Projects in VS2005.NET                ManagementBaseObject outParameter 
= null;
Creating Setup and Deployment Projects in VS2005.NET                outParameter 
= oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET                
//start the web site.
Creating Setup and Deployment Projects in VS2005.NET
                string serverName = "W3SVC/" + serverID;
Creating Setup and Deployment Projects in VS2005.NET                ManagementObject webSite 
= new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + ServerName + "'"), null);
Creating Setup and Deployment Projects in VS2005.NET                webSite.InvokeMethod(
"Start"null);
Creating Setup and Deployment Projects in VS2005.NET                
return (string)outParameter.Properties["ReturnValue"].Value;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch (Exception exp)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
return exp.Message;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
public ManagementObject CreateServerBinding(string HostName, string IP, string Port)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
try
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                ManagementClass classBinding 
= new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);
Creating Setup and Deployment Projects in VS2005.NET                ManagementObject serverBinding 
= classBinding.CreateInstance();
Creating Setup and Deployment Projects in VS2005.NET                serverBinding.Properties[
"Hostname"].Value = HostName;
Creating Setup and Deployment Projects in VS2005.NET                serverBinding.Properties[
"IP"].Value = IP;
Creating Setup and Deployment Projects in VS2005.NET                serverBinding.Properties[
"Port"].Value = port;
Creating Setup and Deployment Projects in VS2005.NET                serverBinding.Put();
Creating Setup and Deployment Projects in VS2005.NET                
return serverBinding;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
catch
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
return null;
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET        
//Install
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET
        Install#region Install
Creating Setup and Deployment Projects in VS2005.NET        
public override void Install(System.Collections.IDictionary stateSaver)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
base.Install(stateSaver);
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET            dir 
= this.Context.Parameters["targetdir"];
Creating Setup and Deployment Projects in VS2005.NET            DBName 
= this.Context.Parameters["dbname"].ToString();
Creating Setup and Deployment Projects in VS2005.NET            ServerName 
= this.Context.Parameters["server"].ToString();
Creating Setup and Deployment Projects in VS2005.NET            AdminName 
= this.Context.Parameters["user"].ToString();
Creating Setup and Deployment Projects in VS2005.NET            AdminPwd 
= this.Context.Parameters["pwd"].ToString();
Creating Setup and Deployment Projects in VS2005.NET            iis 
= this.Context.Parameters["iis"].ToString();
Creating Setup and Deployment Projects in VS2005.NET            port 
= this.Context.Parameters["port"].ToString();
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET            
this.sqlConn.ConnectionString = "Packet size=4096;User ID=" + AdminName + ";Password=" + AdminPwd + ";Data Source=" + ServerName + ";Persist Security Info=False;Integrated Security=false";
Creating Setup and Deployment Projects in VS2005.NET            
//this.sqlConn.ConnectionString = "Packet size=4096; Data Source=" + ";database=master; Persist Security Info=False; Integrated Security=SSPI";
Creating Setup and Deployment Projects in VS2005.NET            
//Execute the sql create database.
Creating Setup and Deployment Projects in VS2005.NET
            if (!CreateDBAndTable(DBName))
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new ApplicationException("A serions error been occured when create database!.");
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET             
//restore the database.
Creating Setup and Deployment Projects in VS2005.NET
            if (!RestoreDB(DBName))
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new ApplicationException("A serions error been occured when backup the data!");
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
// create web site
Creating Setup and Deployment Projects in VS2005.NET
            Connect();
Creating Setup and Deployment Projects in VS2005.NET            
string serverID = GetNextOpenID().ToString();
Creating Setup and Deployment Projects in VS2005.NET            
string serverComment = "HBSTP";
Creating Setup and Deployment Projects in VS2005.NET            
string defaultVrootPath = this.Context.Parameters["targetdir"];
Creating Setup and Deployment Projects in VS2005.NET            
if (defaultVrootPath.EndsWith(@"""))
Creating Setup and Deployment Projects in VS2005.NET            {
Creating Setup and Deployment Projects in VS2005.NET                defaultVrootPath = defaultVrootPath.Substring(0, defaultVrootPath.Length - 1);
Creating Setup and Deployment Projects in VS2005.NET            }
Creating Setup and Deployment Projects in VS2005.NET            string HostName = this.Context.Parameters[
"hostname"];
Creating Setup and Deployment Projects in VS2005.NET
            string IP = this.Context.Parameters["ip"];
Creating Setup and Deployment Projects in VS2005.NET            
string Port = port;
Creating Setup and Deployment Projects in VS2005.NET            
string sReturn = CreateWebSite(serverID, serverComment, defaultVrootPath, HostName, IP, Port);
Creating Setup and Deployment Projects in VS2005.NET
Creating Setup and Deployment Projects in VS2005.NET            
// modify web.config
Creating Setup and Deployment Projects in VS2005.NET
            if (!WriteWebConfig())
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new ApplicationException("setting the connection string falied");
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
// write the registrykey!
Creating Setup and Deployment Projects in VS2005.NET
            WriteRegistryKey();        
Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Uninstall did not complete#region Uninstall did not complete
Creating Setup and Deployment Projects in VS2005.NET        
public override void Uninstall(IDictionary savedState)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET        
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET            
if (savedState == null)
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
throw new ApplicationException("Uninstall failed!");
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET            
else
Creating Setup and Deployment Projects in VS2005.NETCreating Setup and Deployment Projects in VS2005.NET            
Creating Setup and Deployment Projects in VS2005.NET{
Creating Setup and Deployment Projects in VS2005.NET                
base.Uninstall(savedState);
Creating Setup and Deployment Projects in VS2005.NET            }

Creating Setup and Deployment Projects in VS2005.NET        }

Creating Setup and Deployment Projects in VS2005.NET        
#endregion

Creating Setup and Deployment Projects in VS2005.NET    }

Creating Setup and Deployment Projects in VS2005.NET}

Creating Setup and Deployment Projects in VS2005.NET

希望高手指点迷津.
感谢
(徐智雄):http://www.cnblogs.com/xuzhixiong/archive/2006/06/27/437056.html
http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx
http://www.c-sharpcorner.com/UploadFile/vishnuprasad2005/SetupProjects12022005022406AM/SetupProjects.aspx
SetupProjects12022005022406AM/SetupProjects.aspx

转载于:https://www.cnblogs.com/SCPlatform/archive/2007/11/27/973965.html