ApplicationEvents.vb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. Imports System.IO
  2. Imports System.Threading
  3. Namespace My
  4. Partial Friend Class MyApplication
  5. Public Shared Sub Main()
  6. Try
  7. '处理启动参数
  8. Dim StartScript As String = ""
  9. For Each Child As String In Application.CommandLineArgs
  10. StartScript = StartScript & Child & " "
  11. Next
  12. If StartScript.EndsWith(" ") Then StartScript = Mid(StartScript, 1, Len(StartScript) - 1)
  13. '判断启动参数头
  14. Dim Data As String = StartScript.Replace(Split(StartScript, ":")(0) & ":", "")
  15. Select Case Split(StartScript, ":")(0)
  16. Case "Set Environment Variable"
  17. '设置环境变量
  18. Environment.SetEnvironmentVariable("Path", Data, EnvironmentVariableTarget.Machine)
  19. Case "Auto Update"
  20. '自动更新
  21. Dim RetryCount As Integer = 0
  22. Dim ProcessId As Integer = Val(Data.Split("|")(1))
  23. Data = Data.Split("|")(0)
  24. Retry:
  25. '关闭PCL进程
  26. Try
  27. Dim Process As Process = Process.GetProcessById(ProcessId)
  28. Process.Kill()
  29. Catch
  30. End Try
  31. Try
  32. Dim f = New Func
  33. f.CopyDerictory(New DirectoryInfo(Data & "PCL\Update\"), New DirectoryInfo(Data))
  34. Directory.Delete(Data & "PCL\Update", True)
  35. Shell(Data & "PCL.exe", AppWinStyle.NormalFocus)
  36. Catch ex As Exception
  37. RetryCount = RetryCount + 1
  38. If RetryCount < 10 Then
  39. Thread.Sleep(1500)
  40. GoTo Retry
  41. End If
  42. MsgBox("自动更新失败:" & ex.ToString, MsgBoxStyle.Critical, "Plain Craft Launcher - PCLAM")
  43. End Try
  44. End Select
  45. Catch ex As Exception
  46. MsgBox("处理失败:" & ex.ToString, MsgBoxStyle.Critical, "Plain Craft Launcher - PCLAM")
  47. End Try
  48. End Sub
  49. End Class
  50. End Namespace
  51. Public Class Func
  52. Public Sub CopyDerictory(ByVal DirectorySrc As DirectoryInfo, ByVal DirectoryDes As DirectoryInfo)
  53. Dim strDirectoryDesPath As String = DirectoryDes.FullName '& "" & DirectorySrc.Name
  54. If Not Directory.Exists(strDirectoryDesPath) Then Directory.CreateDirectory(strDirectoryDesPath)
  55. Dim f, fs() As FileInfo
  56. fs = DirectorySrc.GetFiles()
  57. For Each f In fs
  58. File.Copy(f.FullName, strDirectoryDesPath & f.Name.ToString, True)
  59. Next
  60. Dim DirSrc, Dirs() As DirectoryInfo
  61. Dirs = DirectorySrc.GetDirectories()
  62. '递归调用自身
  63. For Each DirSrc In Dirs
  64. Dim DirDes As New DirectoryInfo(strDirectoryDesPath)
  65. CopyDerictory(DirSrc, DirDes)
  66. Next
  67. End Sub
  68. End Class