利用.NET技術實現零接觸部署(2) 在應用程式中動態的載入Assembly完成自動更新部署 文/陳明倫 -----box 程式----- Public Sub WalkAssembly(ByVal assemblyName As String) Dim Asm As [Assembly] = System.Reflection.Assembly.Load(assemblyName) For Each m As [Module] In Asm.GetModules For Each t As Type In m.GetTypes For Each mi As MemberInfo In t.GetMembers Console.WriteLine("{0}->{1}.{2}",mi.MemberType,t, mi.Name) Next Next Next End Sub -----end----- -----box 程式----- Private Sub btnListMethods_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListMethods.Click Dim PType As Type = Asm.GetType("DynamicAssembly.Person") Dim saveMi As MethodInfo = PType.GetMethod("Save") Dim savePars() As ParameterInfo = saveMi.GetParameters Console.WriteLine("{0}方法參數成員有{1}個", saveMi.Name, savePars.Length) Dim par As ParameterInfo For Each par In savePars Console.WriteLine("參數名稱:{0}的型別是{1},位於第{2}個", par.Name, par.ParameterType, par.Position) Next End Sub End Class -----end----- -----box 程式----- Imports System.Reflection Imports System.Net Imports System.io Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form 設計工具產生的程式碼 " … #End Region Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click Dim strUrlFile, strMsg, strOldFile, strNewFile As String strUrlFile = "http://192.168.1.1/Com/Version.dll" strMsg = "" strNewFile = "Temp.dll" strOldFile = "Version.dll" Dim bolContinue As Boolean = True Dim myRequest As HttpWebRequest Try myRequest = CType(WebRequest.Create(strUrlFile), HttpWebRequest) Catch ex1 As NotSupportedException strMsg = "NotSupportedException" & ex1.Message bolContinue = False Catch ex2 As ArgumentException strMsg = "ArgumentException" & ex2.Message bolContinue = False Catch ex3 As UriFormatException strMsg = "UriFormatException" & ex3.Message bolContinue = False Catch ex As Exception strMsg = "Exception" & ex.Message bolContinue = False End Try If bolContinue = True Then MessageBox.Show("發生內部錯誤:" & strMsg) Exit Sub End If Dim myResponse As HttpWebResponse Try myresponse = CType(myRequest.GetResponse, HttpWebResponse) Catch ex1 As WebException strMsg = "未找到該檔或連線失敗:" & ex1.Message If File.Exists(strOldFile) Then strMsg &= "載入現有檔案." MessageBox.Show(strMsg) LoadMeDLL(strOldFile) Else strMsg &= "不存在檔案: " & strOldFile strMsg &= "請檢查…." MessageBox.Show(strMsg) End If Catch ex As Exception strMsg = "Exception" & ex.Message MessageBox.Show(strMsg) End Try Dim receiveStream As Stream = myresponse.GetResponseStream Dim ofStream As FileStream = New FileStream(strNewFile, FileMode.Create) Dim streamBytes As Byte() Dim iLength As Int32 = CType(myresponse.ContentLength, Int32) If receiveStream.CanRead Then receiveStream.Read(streamBytes, 0, iLength) ofStream.Write(streamBytes, 0, iLength) Else MessageBox.Show("串流讀取失敗。") End If ofStream.Close() myresponse.Close() Try If File.Exists(strOldFile) Then File.Delete(strOldFile) End If File.Move(strNewFile, strOldFile) Catch ex As Exception MessageBox.Show(ex.ToString()) File.Delete(strNewFile) End Try LoadMeDLL(strOldFile) End Sub Public Sub LoadMeDLL(ByVal strFile As String) Dim simpAsm As [Assembly] = System.Reflection.Assembly.LoadFrom(strFile) Dim frmTemp As Form = CType(simpAsm.CreateInstance("Version.F01"), Form) frmTemp.Show() End Sub -----end-----