Jump to content

kill process

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
ostudioo

ostudioo

    Newbie

  • Members
  • PipPip
  • 10 posts
im currently using this code..

using System.Diagnostics;

foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower() == "star")
if (p.ProcessName.ToLower() == "firefox")

p.Kill();
}
}

star and firefox are just examples of the .exe(s) running..
it doesn't quite work when I add another line of "if (p.ProcessName..etc)

also with the firefox line, how can I check for a specific website opened with firefox? ex.. "firefox", "www.google.com" ?
thanks ahead of time.

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Your code logic is wrong. What you're saying is this:
If the process is called star, then check if the process is called firefox as well. If so, kill the process.
You see the problem? :)

Consider using a switch statement instead of multiple Ifs.
[highlight=csharp]
switch (p.ProcessName.ToLower())
{
case "star": {}
case "firefox": {}
case "other":
{
p.Kill;
break;
}
}
[/highlight]
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
ostudioo

ostudioo

    Newbie

  • Members
  • PipPip
  • 10 posts
thanks for the explanation, i understand what i did wrong now. however the code that you gave me doesn't seem to work, i get compile errors.

private void button1_Click(object sender, EventArgs e)
        {
            switch (p.ProcessName.ToLower())
            {
                case "firefox": {}
                case "star": {}
                    {
                        p.Kill;
                        break;
                    }
            }
        }

intellisense is telling "p" doesn't exist.

#4
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
I would use your old code but just use an or in your if statement.
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower() == "star" || p.ProcessName.ToLower() == "firefox")

p.Kill();
}
that should fix your problem

#5
ostudioo

ostudioo

    Newbie

  • Members
  • PipPip
  • 10 posts
thx, so how would i check if a certain website is running with firefox.

ex: p.ProcessName.ToLower() == "firefox", "www.google.com")

??

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

ostudioo said:

intellisense is telling "p" doesn't exist.
You still need to define p as a Process. I meant just replace the If bit with my code.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
ostudioo

ostudioo

    Newbie

  • Members
  • PipPip
  • 10 posts
I see.. well can anyone help me with my other question? How to find a certain website is running on firefox..

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If you can get the title of the process (one of p's properties, I suppose), then it usually says the name of the site in the address bar. If not, I'm sure there's an API you could automate.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
ostudioo

ostudioo

    Newbie

  • Members
  • PipPip
  • 10 posts
can u give an example of the code..?

:confused:

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I couldn't be sure. You know when you type "p" and then type a dot? It shows you a list of members. Take a look for a name one.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums