Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary) '重写install方法() Dim file As System.IO.File If file.Exists("C:\Program Files\Microsoft SQL Server\MSSQL\Data\Ksxt_data.mdf") = True Then Exit Sub MyBase.Install(stateSaver) Dim CheckedDir As System.IO.Directory If CheckedDir.Exists("C:\Program Files\Microsoft SQL Server\MSSQL\Data") = False Then CheckedDir.CreateDirectory("C:\Program Files\Microsoft SQL Server\MSSQL\Data") End If Dim FullPath As String Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim strConfigLoc As String strConfigLoc = Asm.Location Dim strTemp As String strTemp = strConfigLoc '提取安装路径 strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), Len(strTemp) - strTemp.LastIndexOf("\")) 'Copy DateBase to computer. If CreatDIR(strTemp) = False Then '失败,反安装 Me.Uninstall(stateSaver) Exit Sub Else End If If InstallDB(strTemp) = False Then '失败,反安装 Me.Uninstall(stateSaver) Exit Sub Else End If '删除数据库临时文件 DeleteDIR("c:\TempDB") DeleteDIR(strTemp + "\TempDB") '' ---------------------将连接字符串写入Web.config----------------------------------- 'Try ' Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config") ' If Not FileInfo.Exists Then ' Throw New InstallException("没有找到配置文件") ' End If ' '实例化XML文档 ' Dim XmlDocument As New System.Xml.XmlDocument() ' XmlDocument.Load(FileInfo.FullName) ' '查找到appSettings中的节点 ' Dim Node As System.Xml.XmlNode ' Dim FoundIt As Boolean = False ' For Each Node In XmlDocument.Item("configuration").Item("appSettings") ' If Node.Name = "add" Then ' If Node.Attributes.GetNamedItem("key").Value = "cnStr" Then ' '写入连接字符串 ' Node.Attributes.GetNamedItem("value").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", Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd")) ' FoundIt = True ' End If ' End If ' Next Node ' If Not FoundIt Then ' Throw New InstallException("web.Config 文件没有包含cnStr连接字符串设置") ' End If ' XmlDocument.Save(FileInfo.FullName) 'Catch ex As Exception ' Throw ex 'End Try End Sub Public Overrides Sub Uninstall(ByVal stateSaver As System.Collections.IDictionary) '执行反安装 '利用反射提取安装路径 MyBase.Uninstall(stateSaver) Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim strConfigLoc As String strConfigLoc = Asm.Location Dim strTemp As String strTemp = strConfigLoc strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), Len(strTemp) - strTemp.LastIndexOf("\")) '删除数据库文件和临时文件 DeleteDIR(strTemp + "\TempDB") DeleteDIR("c:\TempDB") End Sub Private Function DeleteDIR(ByVal path As String) As Boolean '删除指定的文件夹 'Dim dir As System.IO.Directory 'If dir.Exists(path) = True Then dir.Delete(path, True) End Function Private Function CreatDIR(ByVal path As String) As Boolean '创建指定的文件夹 Dim Files As System.IO.File Dim Dirs As System.IO.Directory Try If Dirs.Exists("c:\TempDB") = False Then Dirs.CreateDirectory("c:\TempDB") 'copy Creat DB files CopyFile(path + "\TempDB", "C:\TempDB") Return True Catch Return False End Try End Function Private Sub CopyFile(ByVal SourceDirName As String, ByVal DestDirName As String) 'copy指定的文件夹的所有文件到目标文件夹(单层)。 Dim dir As System.IO.Directory Dim File As System.IO.File Dim sPath, oPath As String Dim I As Integer For I = 0 To dir.GetFiles(SourceDirName).Length - 1 sPath = dir.GetFiles(SourceDirName).GetValue(I).ToString oPath = Microsoft.VisualBasic.Right(sPath, Len(sPath) - Len(SourceDirName)) File.Copy(sPath, DestDirName + oPath, True) Next End Sub Private Function InstallDB(ByVal path As String) As Boolean '安装数据库,调用自动批处理。 'Dim CheckedDir As System.IO.Directory 'If CheckedDir.Exists(“C:\Program Files\Microsoft SQL Server\MSSQL\Data”) = False Then 'CheckedDir.CreateDirectory(“C:\Program Files\Microsoft SQL Server\MSSQL\Data”) 'End If Try Shell("c:\TempDB\Restore.bat", AppWinStyle.Hide, True) Catch End Try End Function End Class
TO: qixiao(七小)(找工作中...) 按照你的方法,我在测试SQLServer连接不成功时,没有没有办法取消安装(反安装) // Override the 'Install' method. public override void Install(IDictionary savedState) { base.Install(savedState); //取得安装时输入的参数 string svName=this.Context.Parameters["svname"]; string DBName=this.Context.Parameters["dbname"];
//测试 SQL Server 连接 bool bTest = testSQLServer(svName,DBName); //如果测试 SQL Server 连接失败,反安装 if (!bTest) { MessageBox.Show ("["+svName+"]["+DBName+"]"+"SQL Sever 连接不成功!!");
'' ---------------------将连接字符串写入Web.config----------------------------------- 'Try ' Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config") ' If Not FileInfo.Exists Then ' Throw New InstallException("没有找到配置文件") ' End If ' '实例化XML文档 ' Dim XmlDocument As New System.Xml.XmlDocument() ' XmlDocument.Load(FileInfo.FullName) ' '查找到appSettings中的节点 ' Dim Node As System.Xml.XmlNode ' Dim FoundIt As Boolean = False ' For Each Node In XmlDocument.Item("configuration").Item("appSettings") ' If Node.Name = "add" Then ' If Node.Attributes.GetNamedItem("key").Value = "cnStr" Then ' '写入连接字符串 ' Node.Attributes.GetNamedItem("value").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", Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd")) ' FoundIt = True ' End If ' End If ' Next Node ' If Not FoundIt Then ' Throw New InstallException("web.Config 文件没有包含cnStr连接字符串设置") ' End If ' XmlDocument.Save(FileInfo.FullName) 'Catch ex As Exception ' Throw ex 'End Try End Sub Public Overrides Sub Uninstall(ByVal stateSaver As System.Collections.IDictionary) '执行反安装 '利用反射提取安装路径 MyBase.Uninstall(stateSaver) Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim strConfigLoc As String strConfigLoc = Asm.Location Dim strTemp As String strTemp = strConfigLoc strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), Len(strTemp) - strTemp.LastIndexOf("\")) '删除数据库文件和临时文件 DeleteDIR(strTemp + "\TempDB") DeleteDIR("c:\TempDB") End Sub Private Function DeleteDIR(ByVal path As String) As Boolean '删除指定的文件夹 'Dim dir As System.IO.Directory 'If dir.Exists(path) = True Then dir.Delete(path, True) End Function Private Function CreatDIR(ByVal path As String) As Boolean '创建指定的文件夹 Dim Files As System.IO.File Dim Dirs As System.IO.Directory Try If Dirs.Exists("c:\TempDB") = False Then Dirs.CreateDirectory("c:\TempDB") 'copy Creat DB files CopyFile(path + "\TempDB", "C:\TempDB") Return True Catch Return False End Try End Function Private Sub CopyFile(ByVal SourceDirName As String, ByVal DestDirName As String) 'copy指定的文件夹的所有文件到目标文件夹(单层)。 Dim dir As System.IO.Directory Dim File As System.IO.File Dim sPath, oPath As String Dim I As Integer For I = 0 To dir.GetFiles(SourceDirName).Length - 1 sPath = dir.GetFiles(SourceDirName).GetValue(I).ToString oPath = Microsoft.VisualBasic.Right(sPath, Len(sPath) - Len(SourceDirName)) File.Copy(sPath, DestDirName + oPath, True) Next End Sub Private Function InstallDB(ByVal path As String) As Boolean '安装数据库,调用自动批处理。 'Dim CheckedDir As System.IO.Directory 'If CheckedDir.Exists(“C:\Program Files\Microsoft SQL Server\MSSQL\Data”) = False Then 'CheckedDir.CreateDirectory(“C:\Program Files\Microsoft SQL Server\MSSQL\Data”) 'End If Try Shell("c:\TempDB\Restore.bat", AppWinStyle.Hide, True) Catch End Try End Function
End Class
(1)我要创建一个WinAPP应用程序吗?
(2)在WinAPP应用程序中添加一个“安装程序类”
(3)在WinAPP应用程序的Form中【上一步】【下一步】怎么写?
http://www.lesun.org/html/2003-03/19082.htm
按照你的方法,我在测试SQLServer连接不成功时,没有没有办法取消安装(反安装)
// Override the 'Install' method.
public override void Install(IDictionary savedState)
{
base.Install(savedState); //取得安装时输入的参数
string svName=this.Context.Parameters["svname"];
string DBName=this.Context.Parameters["dbname"];
//测试 SQL Server 连接
bool bTest = testSQLServer(svName,DBName); //如果测试 SQL Server 连接失败,反安装
if (!bTest)
{
MessageBox.Show ("["+svName+"]["+DBName+"]"+"SQL Sever 连接不成功!!");
this.Rollback(savedState); //********* 此处的Rollback() 或 Uninstall() 没有效果
}
}