cesarini Posted March 28, 2014 Report Share Posted March 28, 2014 Boas galera; Tenho uma aplicação que tem uma rotina de atualização online, verifica a versão se a do site for maior baixa. O problema é que não funciona no windows XP e tenho alguns clientes que ainda usam. Vou postar a classe aqui abaixo caso consigam me ajudar agradeço. Imports System.IO Imports System.Net Public Class ClassAtualizacaoSistema ' ------------------------------------------------------ ' Informação a alterar ' ------------------------------------------------------ Private updateServer As String = "------------------------" Private updateFileName As String = "------------------------------" Private updateUserName As String = "---------------------------" Private updatePassword As String = "-----------------------------" Private msgboxTitle As String = "Atualização da Aplicação" Private connTimeout As Integer = 8000000 ' ------------------------------------------------------ ' Constantes Private Const delExtension As String = ".delete" Private Const uptExtension As String = ".update" ' Variáveis Private MyWebClient As New WebClient Private txtInfo As String Private txtInfoArray() As String Private appFullName As String Private appDirectory As String Private appName As String ''' <summary> ''' Formatação da versão do ficheiro ''' </summary> ''' <param name="Version">String com a versão a formatar</param> Private Function GetVersion(ByVal Version As String) As String Dim x() As String = Split(Version, ".") Return String.Format("{0:00000}{1:00000}{2:00000}{3:00000}", Int(x(0)), Int(x(1)), Int(x(2)), Int(x(3))) End Function ''' <summary> ''' Apaga ficheiros antigos (caso existam) ''' </summary> ''' <param name="FileName">Nome do ficheiro</param> Private Sub DeleteFile(ByVal FileName As String) Try Dim sFile As String = Dir(FileName) Do While sFile <> "" Try File.Delete(sFile) Catch ex As Exception End Try sFile = Dir() Loop Catch ex As Exception ' ignora o erro End Try End Sub ''' <summary> ''' Inicia a verificação/update do ficheiro caso esteja disponível ''' </summary> Public Sub CheckUpdate() ' Informação da aplicação appFullName = Application.ExecutablePath.ToString appDirectory = Application.StartupPath.ToString + "\" appName = Application.ProductName.ToString ' Elimina os ficheiros antigos Call DeleteFile(appFullName + delExtension) ' Apaga updates antigos Call DeleteFile(appFullName + uptExtension) Try ' Verifica a existência do servidor onde está o ficheiro txt If Not My.Computer.Network.Ping(updateServer) Then Exit Sub ' Descarrega o ficheiro TXT para uma variável Dim networkCredentials As New Net.NetworkCredential() With networkCredentials .UserName = updateUserName .Password = updatePassword End With MyWebClient.Credentials = networkCredentials txtInfo = MyWebClient.DownloadString(updateFileName.ToString) ' Separa a variável em linhas txtInfoArray = txtInfo.Split(vbNewLine) ' Verifica a versão do ficheiros (original e update) Dim fInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(appFullName) Dim appVersion As String = GetVersion(fInfo.FileMajorPart.ToString + "." + fInfo.FileMinorPart.ToString + "." + fInfo.FileBuildPart.ToString + "." + fInfo.FilePrivatePart.ToString) Dim UpdateVersion As String = GetVersion(txtInfoArray(0)) ' Verifica se é necessário fazer a actualização Dim UpgradeRequired As Boolean = UpdateVersion > appVersion ' Caso seja necessário actualizar If UpgradeRequired Then ' Confirma a intenção de actualizara aplicação Dim msgStart As String = "Existe uma nova atualização do programa." + vbCrLf + vbCrLf + "Deseja efectuar a actualização agora ?" Dim resultStart As DialogResult = MessageBox.Show(msgStart, msgboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) If resultStart <> DialogResult.Yes Then Exit Sub End If Application.DoEvents() ' Descarrega o ficheiro principal Dim updateName As String = txtInfoArray(1).Substring(txtInfoArray(1).LastIndexOf("/") + 1) My.Computer.Network.DownloadFile(txtInfoArray(1).ToString, appDirectory + updateName, _ updateUserName, updatePassword, False, connTimeout, True) ' Caso existam mais ficheiros If txtInfoArray.Length - 1 >= 2 Then ' Descarrega o(s) ficheiro(s) que estão na lista For x As Byte = 2 To txtInfoArray.Length - 1 Try ' Caso a linha não esteja em branco If Not String.IsNullOrEmpty(txtInfoArray(x).Trim) Then ' Nome e localização dos ficheiro Dim updateFile As String = txtInfoArray(x).Trim Dim updateFiles As String = txtInfoArray(x).Substring(txtInfoArray(x).LastIndexOf("/") + 1) ' Inicia o Download do ficheiro My.Computer.Network.DownloadFile(updateFile, appDirectory + updateFiles, _ updateUserName, updatePassword, False, connTimeout, True) End If Catch ex As Exception ' Ignora os erro ... End Try Next End If ' Verifica se está disponível o novo ficheiro If File.Exists(appDirectory + updateName) Then ' Renomeia o principal e depois o ficheiro a actualizar With My.Computer.FileSystem .RenameFile(appFullName, appName + ".exe" + delExtension) .RenameFile(appDirectory + updateName, appName + ".exe") End With ' Mensagem final de actualização Dim msgFinish As String = "Terminou a actualização da aplicação." + vbCrLf + vbCrLf + "Deseja reiniciar o programa agora ?" Dim resultFinish As DialogResult = MessageBox.Show(msgFinish, msgboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information) If resultFinish = MsgBoxResult.Yes Then Application.DoEvents() Application.Restart() End If End If End If Catch ex As Exception MessageBox.Show("Ocorreu um erro ao atualizar o programa. Mensagem original: " + ex.Message, msgboxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class Link to comment Share on other sites More sharing options...
nelsonr Posted March 28, 2014 Report Share Posted March 28, 2014 Podes ser mais específico, que parte é que não funciona? Link to comment Share on other sites More sharing options...
cesarini Posted March 31, 2014 Author Report Share Posted March 31, 2014 Este código que enviei funciona perfeitamente em Windows Vista, Seven e Oito. Mas no XP não da erro nenhum mas ele também não atualiza. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now