Can't launch .exe - vb.net

Trying to make a "world of warcraft" launcher.
How it should work:
When you press " Start Authserver " you get to locate the authserver.exe. the location is then saved so you don't have to do that twice.
Once pressed, it obviously should launch the authserver.exe.
What happens:
When you press " Start Authserver " it opens and instantly closes with error:
Error, couldn't open cause authserver.conf couldn't be located.
Notice:
Authserver.exe and authserver.conf is in the same folder, I'm able to launch it manually.
Mort helped me with similar problem yesterday, i hoped that the same VB code would work since that would seem obvious.
Private filePath As String = String.Empty
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
If filePath.Length = 0 Then
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog()
If diagResult = Windows.Forms.DialogResult.OK Then
filePath = OpenFileDialog1.FileName
If filePath.ToUpper.EndsWith("WOW.EXE") Then
Process.Start(filePath)
Else
MessageBox.Show("Wrong file selected!")
filePath = String.Empty
End If
End If
Else
Process.Start(filePath)
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub
Anyone got an idea?
In advance: Thanks.

Try something like this:
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "World of Warcraft (WOW.EXE)|WOW.EXE"
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog
If diagResult = Windows.Forms.DialogResult.OK Then
Dim p As New Process
Dim fn As New System.IO.FileInfo(OpenFileDialog1.FileName)
p.StartInfo.WorkingDirectory = fn.DirectoryName
p.StartInfo.FileName = fn.Name
p.Start()
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub

Related

Debugging error: "Object reference not set to an instance of an object"

Introduction (Warning: Bad English)
Hello everybody. I need some help with my program.
So, basically my program is made for downloading one file and extracting it to known path, for example C:\Users\ProfileName\Documents\Windward. This path is suitable for most part of people. But for some people it's wrong path (Because file need's to be installed in Documents\Windward folder). So i decided to make changeable path. I thought I making everything right, but something gone wrong. And i thing something is frong with this: Dim path As String = TextBox1.Text & "\Localization.zip", but i don't know how to fix it.
Please help me!
Error:
An unhandled exception of type System.InvalidOperationException occurred in AutoDownloadV2.exe
Additional information: Error in form creating. Exception.InnerException. Error: Object reference not set to an instance of an object.
After Debug I recieve this:
in AutoDownloadV2.My.MyProject.MyForms.Create__Instance__[T](T Instance) in :string 190
in AutoDownloadV2.My.MyApplication.OnCreateMainForm() в B:\Projects\Progs\AutoDownloadV2\AutoDownloadV2\AutoDownloadV2\My Project\Application.Designer.vb:string 35
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
in AutoDownloadV2.My.MyApplication.Main(String[] Args) in :string 81 The program '[5452] AutoDownloadV2.exe' has exited with code 0 (0x0).
Here is my code (I have removed the useless part of code.)
Public Class Form1
Public WithEvents download As WebClient
Dim path As String = TextBox1.Text & "\Localization.zip"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "C:\Users\" & SystemInformation.UserName & "\Documents\Windward"
ProgressBar1.Value = 0
CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Try
My.Computer.FileSystem.DeleteFile(path)
Catch ex As Exception
End Try
download = New WebClient
download.DownloadFileAsync(New Uri("http://http://exsite.example"), path)
Catch ex As Exception
MsgBox("Error! " & ex.Message)
End Try
End Sub
Private Sub download_DownloadProgressChanged(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles download.DownloadProgressChanged
Try
Label3.Text = "Downloaded : " & e.BytesReceived / 1024 & " kb / " & e.TotalBytesToReceive / 1024 & " kb "
Label4.Text = ProgressBar1.Value & "%"
ProgressBar1.Value = e.ProgressPercentage
Catch ex As Exception
MsgBox("Error! " & ex.Message)
End Try
If e.BytesReceived = e.TotalBytesToReceive Then
Unzip()
End If
End Sub
Public Sub Unzip()
Dim startPath As String = path
Dim zipPath As String = path
Dim extractPath As String = ("C:\Users\" + SystemInformation.UserName + "\Documents\Windward\")
If My.Computer.FileSystem.FileExists(extractPath + "mods\translateMod\Localization.txt") Then
My.Computer.FileSystem.DeleteFile(extractPath + "mods\translateMod\Localization.txt")
Try
My.Computer.FileSystem.DeleteFile(extractPath + "mods\translateMod\Version.txt")
Catch e As Exception
End Try
ZipFile.ExtractToDirectory(zipPath, extractPath)
My.Computer.FileSystem.DeleteFile(path)
Else
ZipFile.ExtractToDirectory(zipPath, extractPath)
My.Computer.FileSystem.DeleteFile(path)
End If
End Sub
End Class
Move the initialization of path inside the Form1_Load after setting the initial value in TextBox1
Dim path As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "C:\Users\" & SystemInformation.UserName & "\Documents\Windward"
path = TextBox1.Text & "\Localization.zip"
ProgressBar1.Value = 0
CheckForIllegalCrossThreadCalls = False
End Sub
The problem is caused by the reference to TextBox1 in the global area of your form class. At that point the TextBox1 is still Nothing (not initialized) thus you get the NRE message (Null Reference Exception)

How to run a code that executes in accidental closing of the program in VB.net

This is my code in executing a code that will set the status of a user to offline if they properly close the program:
Private Sub frmMainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MsgBox("Are you sure you want to exit this application?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Exit?") = MsgBoxResult.Yes Then
Update_UserStatus(iUserID, 0) 'update the Status of user to Offline
End
End If
e.Cancel = True
End Sub
Public Sub Update_UserStatus(iUserID As Integer, isOnline As Integer)
Dim strQ As String = String.Empty
strQ = "update tbl_user set isOnline = " & isOnline & " where ID = " & iUserID & ""
Try
If con.State = ConnectionState.Closed Then con.Open()
cmd = New MySqlCommand(strQ, con)
cmd.ExecuteNonQuery()
cmd = Nothing
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
But how can I run this code when the program accidentally close, for example when I open the task manager and force to close the program there. Thank you in advance
You can handle the application threadexception event
Public Shared Sub Main()
' Add the event handler for handling UI thread exceptions to the event.
AddHandler Application.ThreadException, AddressOf ErrorHandlerForm.Form1_UIThreadException
End Sub
' Handle the UI exceptions by showing a dialog box, and asking the user whether
' or not they wish to abort execution.
Private Shared Sub Form1_UIThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs)
Dim result As System.Windows.Forms.DialogResult = _
System.Windows.Forms.DialogResult.Cancel
Try
result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception)
Catch
Try
MessageBox.Show("Fatal Windows Forms Error", _
"Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
Finally
Application.Exit()
End Try
End Try
' Exits the program when the user clicks Abort.
If result = DialogResult.Abort Then
Application.Exit()
End If
End Sub
here you can find a Source Code about how to use it

Error : file is being used by another process

I have a problem where it says that a file is already being used by another process
But I am pretty sure it's not being used anywhere else..
Imports System.IO
Public Class Browse
Private Sub add_Click(sender As Object, e As EventArgs) Handles add.Click
Dim Files As New OpenFileDialog
Dim Folder As New FolderBrowserDialog
Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
Dim FileP As String = ""
Dim list() As String = IO.File.ReadAllLines(Filelist)
If fileb.Checked Then
Try
If System.IO.File.Exists(Filelist) = True And Files.ShowDialog = Windows.Forms.DialogResult.OK Then
FileP = Files.FileName
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If folderb.Checked Then
Try
If System.IO.File.Exists(Filelist) = True And Folder.ShowDialog = Windows.Forms.DialogResult.OK Then
FileP = Folder.SelectedPath
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
pathtxt.Text = FileP
End Sub
Private Sub ok_Click(sender As Object, e As EventArgs) Handles ok.Click
Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
Dim writer As StreamWriter = New StreamWriter(Filelist)
Dim FileP As String = ""
Try
If pathtxt.Text = "" Then
MsgBox("No folder or file has been choosen")
Else
writer = File.AppendText(Filelist)
writer.WriteLine(pathtxt.Text)
pathtxt.Text = FileP
writer.Close()
Me.Hide()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cancel_Click(sender As Object, e As EventArgs) Handles cancel.Click
Me.Hide()
End Sub
Private Sub Browse_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Thanks in advance!
It was because of this:
Dim writer As StreamWriter = New StreamWriter(Filelist)
i changed it to
Dim writer As StreamWriter
and it work like a charm.. dont know if it is the right way.. but it worked.

FolderBrowserDialog open again on cancel

When i open the FolderBrowserDialog then click cancel it reopens again.
But, On the second FolderBrowserDialog when you click cancel again it properly closes.
And when you select a path on the second FolderBrowserDialog, It does or returns nothing
Can i stop the second FolderBrowserDialog on appearing when i click on cancel on the first?
I dont know why is it appearing. thanks in advance.
here is my code:
Dim apppath
Try
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
FolderBrowserDialog1.SelectedPath = "C:\"
FolderBrowserDialog1.Description = "Select File Location Path"
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = FolderBrowserDialog1.SelectedPath
ElseIf FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
MessageBox.Show(apppath)
Catch ex As Exception
MessageBox.Show("Invalid Location")
Exit Sub
End Try
Try something like this
Dim result as Windows.Forms.DialogResult = FolderBrowserDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK Then
apppath = FolderBrowserDialog1.SelectedPath
ElseIf result = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If

My Second BackgroundWorker won't work

I have created 2 BackgroundWorkers and my second BackgroundWorker seem to not work at all, i have placed a messagebox indicator under "Private Sub BackgroundWorker2_DoWork" and it was triggered but the codess under it were not. Here is the complete code of my BackgroundWorker that is having problems. Is there something that is causing this?
i really need 2 BackgroundWorkers for my program as it is processing tons of files which makes the application hang-up.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
If BackgroundWorker2.IsBusy <> True Then
BackgroundWorker2.RunWorkerAsync()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim worker1 As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Try
MessageBox.Show("the program was able to open me")
'this message box above was able to display but the codes below were not processed
Dim Stream As System.IO.FileStream
Dim Index As Integer = 0
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\work\base tremble"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
'This line opens the file the user selected and sets the stream object
Stream = openFileDialog1.OpenFile()
If (Stream IsNot Nothing) Then
'create the reader here and use the stream you got from the file open dialog
Dim sReader As New System.IO.StreamReader(Stream)
Do While sReader.Peek >= 0
ReDim Preserve eArray(Index)
eArray(Index) = sReader.ReadLine
RichTextBox3.Text = eArray(Index)
Index += 1
worker1.ReportProgress(Index)
'Delay(2)
Loop
Label1.Text = "0/" & eArray.Length & ""
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Finally
If (Stream IsNot Nothing) Then
Stream.Close()
End If
End Try
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Try
'Label1.Text = e.ProgressPercentage.ToString()
Me.ProgressBar2.Value = e.ProgressPercentage
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
i have sperated OpenFileDialog into a different button and i processed the retreiving storing of data into the array in the backgroundworker and it works now.
Thanks for the headsup about OpenFileDialog not allowed in backgroundworker it gave me a hint.