Opening up process and grabbing window title. Where did I go wrong?

Posted by user1632018 on Stack Overflow See other posts from Stack Overflow or by user1632018
Published on 2012-09-15T01:58:52Z Indexed on 2012/09/15 3:38 UTC
Read the original article Hit count: 139

Filed under:
|

In my application I allow for the users to add a program from a open file dialog, and it then adds the item to a listview and saves the items location into the tag. So what I am trying to do is when the program in the listview is selected and the button is pressed, it starts a timer and this timer checks to see if the process is running, and if it isn't launches the process, and once the process is launched it gets the window title of the process and sends it to a textbox on another form.

EDIT: The question is if anyone can see why it is not working, by this I mean starting the process, then when it's started closing the form and adding the process window title to a textbox on another form. I have tried to get it working but I can't. I know that the process name it is getting is right I think my problem is to do with my for loop. Basically it isn't doing anything visible right now.

I feel like I am very close with my code and im hoping it just needs a couple minor tweaks. Any help would be appreciated. Sorry if my coding practices aren't that great, im pretty new to this.

EDIT:I thought I found a solution but it only works now if the process has been started already. It won't start it up.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       Dim s As String = ListView1.SelectedItems(0).Tag
    Dim myFile As String = Path.GetFileName(s)
    Dim mu As String = myFile.Replace(".exe", "").Trim()
    Dim f As Process
    Dim p As Process() = Process.GetProcessesByName(mu)

    For Each f In p
        If p.Length > 0 Then
            For i As Integer = 0 To p.Length - 1
                ProcessID = (p(i).Id)
                AutoMain.Name.Text = f.MainWindowTitle
                Timer1.Enabled = False
                Me.Close()
            Next
        Else
            ProcessID = 0
            End If
        If ProcessID = 0 Then
            Process.Start(myFile)
        End If
    Next
    End Sub

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about process