1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development C# Help please

Discussion in 'Software' started by NikoBellic, 21 Jul 2010.

  1. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    Is there any simple code that I can use similar to the code below which can close a running exe file rarther than starting it?:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("explorer");
            }
     
  2. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    You need to do some thing like this:

    Code:
    class Form
    {
            private Process p;
    
            private void button1_Click(object sender, EventArgs e)
            {
                p = System.Diagnostics.Process.Start("explorer");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                p.Close();
            }
    }
    
     
  3. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    No luck unfortunately...

    I just get the following error

    Code:
    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)
     
  4. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    Process.GetProcesses() should return an Arraylist of processes that can run.
    you can then iterate through it, and .Kill() the appropriate one by name comparision.
    i.e.:
    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] PArray = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in PArray)
                {
                    if (p.ProcessName.ToLower() == "notepad")
                    {
                        p.Kill();
                    }
                }
    
            }
    The issue you have with the above example is that you'd need to declare Process as System.Diagnostics.Process rather than just Process, or include "using System.Diagnostics" beforehand.

    secondly, the above example would only close the task it created on Button1 press, where mine would close (all instances of) Notepad.
    Pick and chose, really.
     
  5. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    Woops, forgot to add the using at the top of the class!

    Code:
    using System.Diagnostics;
    
    class Form
    {
            private Process p;
    
            private void button1_Click(object sender, EventArgs e)
            {
                p = Process.Start("explorer");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                p.Close();
            }
    }
    
    That means you don't need to type System.Diagnostics in front of Process every time you want to use the process class (only within that code file though!).
     
  6. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    Yeah... using the namespace like that would make it work.
    To give a better example would really require more info though.
     
  7. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    No luck unfortunately

    the start of my original code is similar to as follows:


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    
    namespace app1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            
    
                private void button1_Click(object sender, EventArgs e)
                {
                    System.Diagnostics.Process.Start("explorer");
                }
    
                private void button2_Click(object sender, EventArgs e)
                {
                    System.Diagnostics.Process.Start("control");
                }
     
  8. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    What exactly do you want to do?
    From what I can tell, you have a form with two buttons.
    Clicking Button1 opens explorer, clicking button2 opens Control panel.

    If you want another two buttons that close the appropriate tasks (i.e. one closes explorer, one closes control panel), then I'd recommend using my method ,and adapting like this:
    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] PArray = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in PArray)
                {
                    if (p.ProcessName.ToLower() == "explorer")
                    {
                        p.Kill();
                    }
                    else 
                    {
                        if (p.ProcessName.ToLower() == "control")
                       {
                           p.Kill();
                       }
                    }
                }
    
    
    or something along those lines (the above is one button that would close either explorer or control panel. Only one of the two at a time...
     
  9. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    The explorer & control commands are just examples,

    I want to make it possible to tweak one of these buttons so instead of launching an app, it can close any exe that is running on my computer (any exe that you can find in the processes tab in taskmanager)
     
  10. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    You need to use BentAnat's method then, but you maynot be able to stop all of Processes running on the PC because of administrator privledges!
     
  11. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    Thanx guys, just got BentAnat's code working now!...

    Sorry for being a "little" N00Bie
     
  12. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    To make it more flexible, you might be able to adda textbox to enter a task name (i.e. "control") and throw that method at a function that closes all tasks with that description.

    you should be able to get reasonably far by just taking the conditionals (i.e. the if(){ and the appropriate } out of it, thus closing all tasks

    mjb is right in stating that you are very likely to run into issues like that, though, as it'll try to close a lot of things (i.e. potentially windows services, etc)
     
  13. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    No probs. it can be a bit confusing if you're not a regular coder, as a little bit of code adaptation is always needed. :)
     
  14. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    Glad you got it working! I have done a bit of reading up on the Kill() command and it geneates an exception if you do not have admin privledges to end the process, so you need to handle them with a try catch statement.

    If you put the try around the p.Kill() it will prevent it from stopping you program when/if these occur. In the catch part I have put told it to show a MessageBox saying it cannot end the Process if this happens.

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] PArray = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in PArray)
                {
                    if (p.ProcessName.ToLower() == "explorer")
                    {
                                 try 
    	            {	        
    	                p.Kill();
    	            }
    	            catch (Exception)
    	            {
                		MessageBox.Show("Could Not End Process!");
    	            }
                    }
                    else 
                    {
                        if (p.ProcessName.ToLower() == "control")
                       {
                                 try 
    	            {	        
    	                p.Kill();
    	            }
    	            catch (Exception)
    	            {
                		MessageBox.Show("Could Not End Process!");
    	            }
                       }
                    }
                }
    
     
  15. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1

Share This Page