我試圖讓運行我的專案成為可能,將自身復制到 update.exe,然后運行該 update.exe 以測驗更新例程。
我遇到的問題是 exefile 已成功復制,但是當 update.exe 然后啟動時,它總是崩潰。
如果我從崩潰中中止了 update.exe 程式,但我的主 exe 仍然運行,那么我可以從資源管理器中啟動 update.exe 并且一切正常。如果從另一個 exefile 復制后啟動 update.exe,我終生無法弄清楚為什么它會崩潰。
這是代碼:
Public Class Updater
Public sFullname As String
Public sExename As String
Public sArguments As String
Public Sub New()
'Constructor
Me.Initiate()
Me.FakeUpdate()
Me.DoUpdate()
'MsgBox(Me.sFullname)
End Sub
Private Sub Initiate()
Dim sCmdLine As String = Environment.CommandLine()
Dim iPos = sCmdLine.IndexOf("""", 2)
Me.sFullname = sCmdLine.Substring(1, iPos - 1).Trim()
Dim iPos2 = sFullname.LastIndexOf("\")
Me.sExename = sFullname.Substring(iPos2 1).Trim()
Me.sArguments = sCmdLine.Substring(iPos 1).Trim()
End Sub
Private Sub FakeUpdate()
'If we start the app with -fakeupdate parameter, copy myself to update.exe, then run update.exe to debug the update process.
If Me.sArguments = "-fakeupdate" Then
FileCopy(Me.sFullname, "update.exe")
Shell("update.exe")
End If
End Sub
Private Sub DoUpdate()
If Me.sExename = "update.exe" Then
MsgBox("DoUpdate called from update.exe")
End If
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Updater As New Updater
End Sub
End Class
該專案配置為使用引數 -fakeupdate 運行
該代碼是一個更大專案的一部分,但我已注釋掉所有其他代碼,但仍然收到此錯誤。
我從 update.exe 得到的錯誤:長度不能小于空。引數名稱:長度
System.ArgumentOutOfRangeException: Length cannot be less than null.
Parameternaam: length
bij System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
bij Todo_List.Updater.Initiate() in S:\VB Projects\Todo List\Todo List\Form1.vb:regel 19
bij Todo_List.Updater..ctor() in S:\VB Projects\Todo List\Todo List\Form1.vb:regel 8
bij Todo_List.Form1.Form1_Load(Object sender, EventArgs e) in S:\VB Projects\Todo List\Todo List\Form1.vb:regel 594
bij System.EventHandler.Invoke(Object sender, EventArgs e)
bij System.Windows.Forms.Form.OnLoad(EventArgs e)
bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bij System.Windows.Forms.Control.CreateControl()
bij System.Windows.Forms.Control.WmShowWindow(Message& m)
bij System.Windows.Forms.Control.WndProc(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bij System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
EDIT: small update, it also goes wrong if I don't even copy the file. Just shell("update.exe") goes wrong. I even created a button on the form to launch it upon click and it fails every time. But if I run it from explorer, all is fine.
uj5u.com熱心網友回復:
這是一種奇怪的行為,甚至可能是一個錯誤,但我想通了。
顯然,如果您使用shell ("program.exe")啟動一個程式,并且在該程式中,您呼叫Environment.CommandLine()它將只是 program.exe,而如果您從資源管理器中雙擊該程式,它將改為 C:\MyPath\program.exe。
為了解決這個問題,我不得不改變我的代碼如下:
Private Sub FakeUpdate()
Dim sShell As String
'If we start the app with -fakeupdate parameter, copy myself to update.exe, then run update.exe to debug the update process.
If Me.sArguments = "-fakeupdate" Then
FileCopy(Me.sFullname, "update.exe")
sShell = My.Computer.FileSystem.CurrentDirectory & "\update.exe"
Shell(sShell)
End If
End Sub
現在它可以正常作業,無論是從資源管理器還是從程式內啟動程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/455661.html
標籤:vb.net
上一篇:多個記憶體映射檔案是否存在錯誤?
