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
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
GetProcessesByNamemethod returns an array ofProcessobjects, not a singleProcess. 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 aKillmethod. Even if there's just one element in that array, you need to get the elements, which areProcessobjects, and callKillon 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 Eachloops in this case. Meaningful variable names would help too: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 Onand ALWAYS leave itOnexcept in specific cases where you need late binding, which may be never. Turn itOnin the project properties and also in the VS options, so it will beOnby default in all future projects. If you ever need to use late binding, you turn itOffat 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:
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.