当服务器端软件版本更新,客户端自动查找新版本并更新, 最好提供源码
解决方案 »
- C#关于动态编译加载dll文件问题
- 多线程的小问题
- Windows Mobile里关于ComboBox的问题
- 深度复制问题!引申到接口问题
- 如何解决C#编程实现远程SQL Server数据库备份的路径问题
- 如何绑定textbox使用dataAdapter和dataSet向数据库里插入数据
- 如何获取编辑框中鼠标选中部分文本
- 菜鸟提问:treeView的滚动条问题,高人帮忙一下
- 怎样在数据库中查询本日、本月、本年的记录?Sql server
- 关于在Datalist等数据控件的模板中 使用含有多选性质的控件的疑惑
- C# 图片层叠顺序
- datagridview绑定一张工资表,其中一列绑定员工的家庭住址用comboBox表示,怎么用代码实现?
记住要用异步来写.
Imports System.Threading
Imports System.IO
Public Class UpDate
Inherits System.Windows.Forms.Form
Dim i As Integer
Dim serverName As String
Dim ExeFileName() As String = New String() {} '要升级的执行文件的名称 Private Sub UpDate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim f As New FileStream(Application.StartupPath & "\erp_system.txt", FileMode.Open)
Dim ff As New StreamReader(f)
Dim fstring As String = ff.ReadLine()
serverName = Split(fstring.ToLower, "<dbname>", -1)(0)
serverName = Replace(serverName, "<server_name>", "")
ff.Close()
f.Close()
'下载服务器上最新的下载列表:zxMatmanage,Module1,Module2...
If File.Exists(Application.StartupPath & "\" & "UpDateList.txt") Then
File.Delete(Application.StartupPath & "\" & "UpDateList.txt")
End If
Try
Dim myWebClient As New WebClient
myWebClient.DownloadFile("http://" & serverName & "/download/capital/UpDateList.txt", Application.StartupPath & "\UpDateList.txt")
myWebClient.Dispose()
Dim Fs As New FileStream(Application.StartupPath & "\" & "UpDateList.txt", FileMode.Open)
Dim Rs As New StreamReader(Fs)
Dim DownLoadList As String = Rs.ReadLine
ExeFileName = Split(DownLoadList, ",", -1)
Rs.Close()
Fs.Close() Catch ex As Exception
If Err.Number > 0 Then
MsgBox(ex.Message & Err.Number & Chr(13) & "下载文件列表失败!", MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "出错提示")
End If
End Try End Sub
Delegate Sub DownLoadIt(ByVal FileName As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Timer1.Enabled = True : Timer1.Interval = 1000 : ProgressBar1.Maximum = 10000 : i = 0
Me.Button1.Visible = False
Dim i As Integer
If UBound(ExeFileName) < 0 Then
MsgBox("下载列表为空,请联系管理员!", MsgBoxStyle.OKOnly, "提示")
Exit Sub
End If
Dim Td(UBound(ExeFileName)) As DownLoadIt
Dim r(UBound(ExeFileName)) As IAsyncResult
For i = 0 To UBound(ExeFileName)
Td(i) = New DownLoadIt(AddressOf Me.UpGradeFunction)
r(i) = Td(i).BeginInvoke(ExeFileName(i), New AsyncCallback(AddressOf Me.AfterDownLoad), Td(i))
Next End Sub Delegate Sub CountFile()
Sub AfterDownLoad(ByVal Ir As IAsyncResult)
' Dim aResult As System.Runtime.Remoting.Messaging.AsyncResult = CType(Ir, System.Runtime.Remoting.Messaging.AsyncResult)
Dim jg As New CountFile(AddressOf Count)
Me.Button1.Invoke(jg)
End Sub
Sub Count()
FileCount = FileCount + 1
If FileCount = ExeFileName.Length Then
Me.Label1.Text = "全部下载完毕,请重新运行系统!"
Thread.Sleep(2000)
Me.Close()
' Application.Exit()
End If
End Sub Sub AfterSucces(ByVal DownLoadFileName As String)
Cursor.Current = Cursors.Default
Label1.Text = DownLoadFileName & "下载成功!"
If Not File.Exists(Application.StartupPath & "\" & DownLoadFileName & "1.exe") Then
MsgBox("下载文件不成功,网络有问题!请重试!", MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, "提示")
Button1.Visible = True
Label1.Text = ""
Exit Sub
End If
'应该从进程中结束掉该应用的所有进程,以防在用,导致更新不成功!
killProcess(DownLoadFileName)
If File.Exists(Application.StartupPath & "\" & DownLoadFileName & ".exe") Then
File.Delete(Application.StartupPath & "\" & DownLoadFileName & ".exe")
End If
If File.Exists(Application.StartupPath & "\" & DownLoadFileName & "1.exe") Then
Rename(DownLoadFileName & "1.exe", DownLoadFileName & ".exe")
'Process.Start(Application.StartupPath & "\" & DownLoadFileName & ".exe")
End If
'由于是异步,所以以下代码在主线程无法弹出提示窗
'fileCount = fileCount + 1
'If fileCount = ExeFileName.Length Then
' MsgBox("全部下载完毕!请重新运行系统!", MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, "系统提示")
' Me.Close()
' Application.Exit()
'End If
End Sub Sub killProcess(ByVal DownLoadFileName As String)
Dim pProcess() As Process
pProcess = Process.GetProcesses()
Dim i As Integer
For i = 0 To pProcess.Length - 1
If UCase(pProcess(i).ProcessName) = UCase(DownLoadFileName) Then
' If MsgBox("kill it now?", MsgBoxStyle.YesNo, "inf") = MsgBoxResult.Yes Then
pProcess(i).Kill() 'kill it
'End If
End If
Next
End Sub
Sub UpGradeFunction(ByVal downloadFilename As String)
Try
Cursor.Current = Cursors.Hand
Dim Filename As String = downloadFilename & ".exe"
Dim DownUrl As String = "http://" & serverName & "/download/capital/" & Filename
Dim myStringWebResource As String = DownUrl
Dim myWebClient As New WebClient
myWebClient.DownloadFile(myStringWebResource, Application.StartupPath & "\" & downloadFilename & "1.exe")
myWebClient.Dispose()
Catch ex As Exception
MsgBox(Err.Description & " " & Err.Number, MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "升级出错提示")
Cursor.Current = Cursors.Default
Exit Sub
End Try
Cursor.Current = Cursors.Default
Label1.Text = downloadFilename & "正在更新..."
Thread.Sleep(500)
AfterSucces(downloadFilename)
End Sub
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
Button1.Visible = False
End Sub Private Sub UpDate_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
Me.Close()
End
End Sub Private Sub UpDate_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If FileCount = ExeFileName.Length Then
MsgBox("全部下载完毕!请重新运行系统!", MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, "系统提示")
Application.Exit()
End If
End Sub
End Class