Advertisement
Guest User

test

a guest
Sep 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. Imports System.IO, System.Net, System.Web, System.Xml, System.Runtime.InteropServices
  2. Public Class FrmUpdate
  3.  
  4. Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
  5. Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
  6.  
  7.  
  8. Dim DataFile As WebClient
  9. Dim DataDownload As String = "http://yansaan.indoweb.xyz/download/FindMyWaifuPortable.html"
  10. Dim msg As String = ""
  11. Dim Out As Integer
  12. Dim updates As Integer
  13. Dim fol As New CreateFolder()
  14. Dim filedownload As String = fol.appDataFMW & "\_data\updates\update.zip"
  15.  
  16. Dim check As Boolean = False
  17.  
  18. Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef conn As Long, ByVal val As Long) As Boolean
  19.  
  20.  
  21. Public Sub CheckForUpdates()
  22. If InternetGetConnectedState(Out, 0) = True Then
  23. Try
  24. Dim ver As String = ""
  25. Dim xmlUpdate As New XmlTextReader("https://onedrive.live.com/download?cid=9675D76E084032AB&resid=9675D76E084032AB%21815&authkey=APPoahifAoJiGZo")
  26. Dim newver As String = ""
  27. Dim desc As String = ""
  28.  
  29. While xmlUpdate.Read()
  30. Dim type = xmlUpdate.NodeType
  31. If xmlUpdate.Name = "version" Then
  32. newver = xmlUpdate.ReadInnerXml.ToString()
  33. End If
  34. If xmlUpdate.Name = "description" Then
  35. desc = xmlUpdate.ReadInnerXml.ToString()
  36. End If
  37. End While
  38.  
  39. Label2.Text = "Update Ver.: " + newver
  40. Dim lastver As String = "0.0.0.8" 'Application.ProductVersion
  41. updates = 1
  42.  
  43. If newver < lastver Then
  44. RichTextBox1.Text = desc
  45. Label3.Text = ""
  46. Else
  47. Label3.Text = "Sudah Terupdate"
  48. RichTextBox1.Text = "Versi anda sudah yang terbaru"
  49. Button1.Enabled = False
  50. End If
  51. Catch ex As Exception
  52. Label3.Text = ""
  53. updates = 0
  54. RichTextBox1.Text = "Internet sedang gangguan, kilk Retry untuk menyambung ulang"
  55. Button1.Text = "Retry"
  56. End Try
  57. Else
  58. Label3.Text = ""
  59. updates = 0
  60. RichTextBox1.Text = "Internet belum terkoneksi, kilk Retry untuk menyambung ulang"
  61. Button1.Text = "Retry"
  62. End If
  63. End Sub
  64.  
  65. Private Sub FrmUpdate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  66. Dim create As New CreateFolder()
  67. If (Not System.IO.Directory.Exists(create.appDataFMW & "\_data")) Then
  68. System.IO.Directory.CreateDirectory(create.appDataFMW & "\_data")
  69. End If
  70.  
  71. If (Not System.IO.Directory.Exists(create.appDataFMW & "\_data\updates")) Then
  72. System.IO.Directory.CreateDirectory(create.appDataFMW & "\_data\updates")
  73. End If
  74.  
  75. IO.File.SetAttributes(create.appDataFMW & "\_data", IO.FileAttributes.Hidden Or
  76. IO.FileAttributes.System)
  77.  
  78. RichTextBox1.ReadOnly = True
  79. RichTextBox1.BackColor = ColorTranslator.FromHtml("#f3f3f3")
  80. Label1.Text = "Curent ver.: " + Application.ProductVersion
  81. CheckForUpdates()
  82.  
  83. End Sub
  84.  
  85. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  86. Dim fileDetail = My.Computer.FileSystem.GetFileInfo(filedownload)
  87.  
  88. If Not File.Exists(filedownload) Then
  89. BackgroundWorker1.RunWorkerAsync()
  90. Else
  91. If fileDetail.Length = 0 Then
  92. BackgroundWorker1.RunWorkerAsync()
  93. Else
  94. Dim hasil As String = MsgBox("Update file sudah tersedia" + Chr(13) + "apakah ingin mendownload ulang?", vbInformation + vbYesNo, "Updating")
  95. If hasil = vbYes Then
  96. BackgroundWorker1.RunWorkerAsync()
  97. ElseIf hasil = vbNo Then
  98. Installing()
  99. End If
  100. End If
  101. End If
  102. End Sub
  103.  
  104. Private Sub Installing()
  105. Dim process As New Process()
  106. process.StartInfo.FileName = "UpdateMyWaifu.exe"
  107. process.StartInfo.Verb = "runas"
  108. process.StartInfo.UseShellExecute = True
  109. process.Start()
  110.  
  111. Form1.Close()
  112. End Sub
  113.  
  114. Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  115.  
  116. ' creating the request and getting the response
  117. Dim theResponse As HttpWebResponse
  118. Dim theRequest As HttpWebRequest
  119. If File.Exists(filedownload) Then
  120. File.Delete(filedownload)
  121. End If
  122.  
  123. Try 'check if the file is exist
  124.  
  125. theRequest = WebRequest.Create(DataDownload)
  126. theResponse = theRequest.GetResponse
  127.  
  128. Catch ex As Exception
  129.  
  130. MessageBox.Show("Update is error" & ControlChars.CrLf &
  131. "Check your connection", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  132. 'we will create the delegate here
  133. ' just wait fo a moment
  134.  
  135. Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  136. Me.Invoke(cancelDelegate, True)
  137. Exit Sub
  138. End Try
  139.  
  140. Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
  141. ' we will create the functions for update the informations
  142. ' just wait for a moment
  143.  
  144.  
  145.  
  146. Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
  147. Me.Invoke(safedelegate, lenght, 0, 0, 0)
  148.  
  149. Dim writestream As New IO.FileStream(Me.filedownload, IO.FileMode.Create)
  150.  
  151. 'Replacement for Stream.Position (webResponse stream doesn't support seek)
  152. Dim nRead As Integer
  153.  
  154. 'To calculate the download speed
  155. Dim speedTimer As New Stopwatch
  156. Dim currentspeed As Double = -1
  157. Dim readings As Integer = 0
  158.  
  159. Do
  160. If BackgroundWorker1.CancellationPending Then 'If user abort download
  161. Exit Do
  162. End If
  163. speedTimer.Start()
  164. Dim readBytes(4095) As Byte
  165. Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  166. nRead += bytesread
  167.  
  168. Dim percent As Short = (nRead * 100) / lenght
  169. Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)
  170.  
  171. ' sorry for it, just replace the variable speed to double
  172. ' lets try it again
  173.  
  174. If bytesread = 0 Then Exit Do
  175.  
  176. writestream.Write(readBytes, 0, bytesread)
  177. speedTimer.Stop()
  178.  
  179. readings += 1
  180. If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  181.  
  182. currentspeed = 20480 / (speedTimer.ElapsedMilliseconds / 1000)
  183. speedTimer.Reset()
  184. readings = 0
  185. End If
  186.  
  187. Loop
  188.  
  189. 'Close the streams
  190. theResponse.GetResponseStream.Close()
  191. writestream.Close()
  192.  
  193. If Me.BackgroundWorker1.CancellationPending Then
  194.  
  195. IO.File.Delete(Me.filedownload)
  196. Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  197. Me.Invoke(canceldelegate, True)
  198. Exit Sub
  199. End If
  200.  
  201. Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  202. Me.Invoke(complatedelegate, False)
  203. End Sub
  204.  
  205. Public Sub DownloadComplate(ByVal cancelled As Boolean)
  206.  
  207. If cancelled Then
  208. Label3.Text = "Check Your Connection"
  209. Else
  210.  
  211. End If
  212.  
  213. End Sub
  214.  
  215. Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
  216.  
  217. Label3.Text = "Downloading Update " & ProgressBar1.Value & "% (" & Math.Round((position / 1024), 2) & " KB / " & Math.Round((lenght / 1024), 2) & " KB..."
  218. 'Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
  219. 'Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
  220. 'Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "
  221.  
  222. 'If speed = -1 Then
  223.  
  224. ' Me.Label5.Text = "Speed : Calculating ..."
  225. 'Else
  226. ' Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
  227. 'End If
  228.  
  229. Me.ProgressBar1.Value = percent
  230.  
  231. End Sub
  232.  
  233. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement