Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Close all application using c#

Close all application using c#

Scheduled Pinned Locked Moved C#
csharphelpquestion
21 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Ankit Rajpoot

    Hello SeMartens, I'm not talking about adding something to the process list, but to an instance of the

    List

    class. Like the following code:

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    List<Process> list = new List<Process>();
    string input="";

            Console.WriteLine("Enter path to a program to start it.");
            while (true)
            {
                input = Console.ReadLine();
                if (input == "exit")
                    break;
                try
                {
                    list.Add(Process.Start(input));
                }
                catch
                {
                    Console.WriteLine("Failed to start " + input);
                }
            }
            foreach (Process process in list)
            {
                if (!process.HasExited)
                    process.Kill();
            }
        }
    }
    

    }

    S Offline
    S Offline
    sandhya14
    wrote on last edited by
    #11

    ok.. see am giving like this.. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"Notepad"); System.Diagnostics.Process listFiles; listFiles = System.Diagnostics.Process.Start(psi); listFiles.Kill(); here what is happening is it is opening a new notepad and closing that but i need to close the application which is already opened... ex:- i have opend 3 browsers and 1 notepad already opened in my system...now am running my application.now if i close my application these 3 browser and 1 notepad also should get closed... thats what am asking how can i add these already opened things in to my list?

    S A 2 Replies Last reply
    0
    • S sandhya14

      ok.. see am giving like this.. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"Notepad"); System.Diagnostics.Process listFiles; listFiles = System.Diagnostics.Process.Start(psi); listFiles.Kill(); here what is happening is it is opening a new notepad and closing that but i need to close the application which is already opened... ex:- i have opend 3 browsers and 1 notepad already opened in my system...now am running my application.now if i close my application these 3 browser and 1 notepad also should get closed... thats what am asking how can i add these already opened things in to my list?

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #12

      Hi, first you could use

      System.Diagnostics.Process.GetProcesses();

      to enumerate all running processes. But closing them could be difficult because you have to distinguish between system processes and application processes. A better way is to get all top-level applications (like the task-manager) and close them. Have a look at this link http://www.neowin.net/forum/lofiversion/index.php/t369213.html[^]. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      C 1 Reply Last reply
      0
      • S sandhya14

        ok.. see am giving like this.. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"Notepad"); System.Diagnostics.Process listFiles; listFiles = System.Diagnostics.Process.Start(psi); listFiles.Kill(); here what is happening is it is opening a new notepad and closing that but i need to close the application which is already opened... ex:- i have opend 3 browsers and 1 notepad already opened in my system...now am running my application.now if i close my application these 3 browser and 1 notepad also should get closed... thats what am asking how can i add these already opened things in to my list?

        A Offline
        A Offline
        Ankit Rajpoot
        wrote on last edited by
        #13

        Use Process.GetProcessByName(). Or If you want to play it more safely, use Process.GetProcessById() method. To obtain the PIDs for the processes you want to end, get a list of all the processes running on your system using Process.GetProcesses(). Then manually traverse the list and find the PID of the processes you want to end, and then end them from there. [Edit]Have a look at the process class and find out what it can do.[^][/Edit] BTW, please answer this time, what are you building, and do you work for some company?

        Excuse me for buttin' in, but I'm interrupt driven.

        1 Reply Last reply
        0
        • A Ankit Rajpoot

          See my post[^]

          S Offline
          S Offline
          sandhya14
          wrote on last edited by
          #14

          Now its working fine....:-) now everything is getting closed..but am getting the shutdown window also after giving this code Process[] processlist = Process.GetProcesses("Avinash"); foreach (Process theprocess in processlist) { theprocess.CloseMainWindow(); } i dont want to get that shutdown window...?

          A 1 Reply Last reply
          0
          • S sandhya14

            Now its working fine....:-) now everything is getting closed..but am getting the shutdown window also after giving this code Process[] processlist = Process.GetProcesses("Avinash"); foreach (Process theprocess in processlist) { theprocess.CloseMainWindow(); } i dont want to get that shutdown window...?

            A Offline
            A Offline
            Ankit Rajpoot
            wrote on last edited by
            #15

            Hi Sandhya, The reason you're getting that shutdown window is that the GetProcesses method gets a list of all running processes on the local computer or the machine name specified as an argument, and this list includes explorer as well. So when you call CloseMainWindow(), the close signal is being sent to all running programs including explorer as well. You should do like given below:

            Process[] processlist = Process.GetProcessesByName("Notepad");

            foreach (Process theprocess in processlist)
            {
            theprocess.CloseMainWindow();
            }

            All the best

            Excuse me for buttin' in, but I'm interrupt driven.

            S 1 Reply Last reply
            0
            • A Ankit Rajpoot

              Hi Sandhya, The reason you're getting that shutdown window is that the GetProcesses method gets a list of all running processes on the local computer or the machine name specified as an argument, and this list includes explorer as well. So when you call CloseMainWindow(), the close signal is being sent to all running programs including explorer as well. You should do like given below:

              Process[] processlist = Process.GetProcessesByName("Notepad");

              foreach (Process theprocess in processlist)
              {
              theprocess.CloseMainWindow();
              }

              All the best

              Excuse me for buttin' in, but I'm interrupt driven.

              S Offline
              S Offline
              sandhya14
              wrote on last edited by
              #16

              In this case only notepad had got closed and rest others are there only..:-( (Explorer,MyComputer.,etc...)

              A 1 Reply Last reply
              0
              • S sandhya14

                In this case only notepad had got closed and rest others are there only..:-( (Explorer,MyComputer.,etc...)

                A Offline
                A Offline
                Ankit Rajpoot
                wrote on last edited by
                #17

                Do you have a yahoo id. If yes, send me an Add Request at ankit_incredible_2006. The problem can be solved more quickly if we have a realtime comm. link.

                Excuse me for buttin' in, but I'm interrupt driven.

                A 1 Reply Last reply
                0
                • A Ankit Rajpoot

                  Do you have a yahoo id. If yes, send me an Add Request at ankit_incredible_2006. The problem can be solved more quickly if we have a realtime comm. link.

                  Excuse me for buttin' in, but I'm interrupt driven.

                  A Offline
                  A Offline
                  Ankit Rajpoot
                  wrote on last edited by
                  #18

                  All right, try like this

                  void CloseApplications(void)
                  {
                  string explorerPath = Path.GetDirectoryName(System.Environment.SystemDirectory);

                       explorerPath = Path.Combine(explorerPath.ToLower(), "explorer.exe");
                  

                  Process[] processes = Process.GetProcesses();
                  foreach(Process process in processes)
                  {
                  try
                  {
                  if (process.MainModule.FileName.ToLower() != explorerPath)
                  process.CloseMainWindow();
                  }
                  catch
                  {
                  // Handle exceptions here.
                  }
                  }
                  }

                  Excuse me for buttin' in, but I'm interrupt driven.

                  S 1 Reply Last reply
                  0
                  • A Ankit Rajpoot

                    All right, try like this

                    void CloseApplications(void)
                    {
                    string explorerPath = Path.GetDirectoryName(System.Environment.SystemDirectory);

                         explorerPath = Path.Combine(explorerPath.ToLower(), "explorer.exe");
                    

                    Process[] processes = Process.GetProcesses();
                    foreach(Process process in processes)
                    {
                    try
                    {
                    if (process.MainModule.FileName.ToLower() != explorerPath)
                    process.CloseMainWindow();
                    }
                    catch
                    {
                    // Handle exceptions here.
                    }
                    }
                    }

                    Excuse me for buttin' in, but I'm interrupt driven.

                    S Offline
                    S Offline
                    sandhya14
                    wrote on last edited by
                    #19

                    ok..thanks 4 ur reply... what is the process name for the window shudown?

                    A 1 Reply Last reply
                    0
                    • S sandhya14

                      ok..thanks 4 ur reply... what is the process name for the window shudown?

                      A Offline
                      A Offline
                      Ankit Rajpoot
                      wrote on last edited by
                      #20

                      It's explorer.exe

                      Excuse me for buttin' in, but I'm interrupt driven.

                      1 Reply Last reply
                      0
                      • S SeMartens

                        Hi, first you could use

                        System.Diagnostics.Process.GetProcesses();

                        to enumerate all running processes. But closing them could be difficult because you have to distinguish between system processes and application processes. A better way is to get all top-level applications (like the task-manager) and close them. Have a look at this link http://www.neowin.net/forum/lofiversion/index.php/t369213.html[^]. Regards Sebastian

                        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                        C Offline
                        C Offline
                        charles henington
                        wrote on last edited by
                        #21

                        {

                                Process\[\] processes = Process.GetProcessesByName("notepad");
                                
                                foreach (Process process in processes)
                                {
                                    process.Kill();
                                }
                        
                            }
                        

                        modified on Thursday, May 13, 2010 1:08 AM

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups