arraysvb.netfor-loopkillkill-process

I want to kill steam.exe and some extra steam process but I don't know how to do it in VB.NET


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim steamkill() As String = {"steamwebhelper.exe", "steam.exe", "steamservice.exe"}
    Dim proc

    For a As Integer = 0 To steamkill.Length Step 1
         proc = Process.GetProcessesByName(steamkill.ElementAt(a))
         proc.Kill()

    Next

End Sub

I keep getting this error:

System.MissingMemberException: 'Public member 'Kill' on type 'Process()' not found.'

I would like to do it in silent mode.

I tried to change for to go in each array item but did not work. I want to kill all steam processes


Solution

  • This issue demonstrates two things perfectly. First of all, you should ALWAYS have Option Strict On. Secondly, you should ALWAYS read the documentation. If you had done the first then this issue would be caught at compile time instead of run time and if you had done the second then you'd know what the issue is and how to fix it.

    As the documentation clearly states, that GetProcessesByName method returns an array of Process objects, not a single Process. It's even in the name, i.e. it's telling you that it's getting multiple processes. As the error message is telling you explicitly, an array does not have a Kill method. Even if there's just one element in that array, you need to get the elements, which are Process objects, and call Kill on each one.

    Ther's no good reason to declare that variable outside the loop in the first place. It's only used inside the loop so declare it inside the loop, then use another loop to access each element. You should be using For Each loops in this case. Meaningful variable names would help too:

    Dim processNames As String() = {"steamwebhelper.exe", "steam.exe", "steamservice.exe"}
    
    For Each processName In processNames
        Dim processes As Process() = Process.GetProcessesByName(processName)
    
        For Each process In processes
            process.Kill()
        Next
    Next
    

    Note that the array variables use plural names while singular names are used elsewhere.

    As I said, to help you avoid situations like this in future, you should turn Option Strict On and ALWAYS leave it On except in specific cases where you need late binding, which may be never. Turn it On in the project properties and also in the VS options, so it will be On by default in all future projects. If you ever need to use late binding, you turn it Off at the file level in only the files that explicitly require it. Even then, you use partial classes to keep the code in those files to an absolute minimum.

    That way, you won't be able to do things like this:

    Dim proc
    

    By not specifying a data type for that variable, you are allowing it to default to type Object, meaning that you will get no Intellisense for the members of the actual type of the object and you are also able to try to access members that don't exist, just as you did, and have it blow up at run time instead of the compiler telling you that it doesn't make sense.