tags:

views:

306

answers:

2

I have a project where I have multiple instances of an app running, each of which was started with different command line arguments. I'd like to have a way to click a button from one of those instances which then shuts down all of the instances and starts them back up again with the same command line arguments.

I can get the processes themselves easily enough through Process.GetProcessesByName(), but whenever I do, the StartInfo.Arguments property is always an empty string. It looks like maybe that property is only valid before starting a process.

This question had some suggestions, but they're all in native code, and I'd like to do this directly from .NET. Any suggestions?

+6  A: 

This is using all managed objects, but it does dip down into the WMI realm:

    foreach (Process p in Process.GetProcesses())
    {
        try
        {
            Console.Write(p.MainModule.FileName + " ");
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + p.Id))
            {
                foreach (ManagementObject @object in searcher.Get())
                {
                    Console.Write(@object["CommandLine"] + " ");
                }

                Console.WriteLine();
            }
        }
        catch (Win32Exception ex)
        {
            if ((uint)ex.ErrorCode != 0x80004005)
            {
                throw;
            }
        }
    }
Jesse C. Slicer
only thing to watch out for is AccessDenied on some of the processes
Paul Farry
Good call, Paul. I'm going to add a bit.
Jesse C. Slicer
A: 

The StartInfo.Arguments is only used when you start the app, it is not a record of the command line arguments. If you start the applications with command line arguments, then store the arguments when they come into your application. In the simplest case, you could store them in a text file, then when you hit the button, shut down all the processes except the one with the button press event. Fire off a new application, and feed it that file in a new command line arg. While the old app shuts down, the new app fires off all the new processes (one for each line in the file) and shuts down. Psuedocode below:

static void Main(string[] args)
{
   if (args.Contains(StartProcessesSwitch))
      StartProcesses(GetFileWithArgs(args))
   else
      WriteArgsToFile();
      //Run Program normally
}

void button_click(object sender, ButtonClickEventArgs e)
{
   ShutDownAllMyProcesses()
}

void ShutDownAllMyProcesses()
{
   List<Process> processes = GetMyProcesses();
   foreach (Process p in processes)
   {
      if (p != Process.GetCurrentProcess())
         p.Kill(); //or whatever you need to do to close
   }
   ProcessStartInfo psi = new ProcessStartInfo();
   psi.Arguments = CreateArgsWithFile();
   psi.FileName = "<your application here>";
   Process p = new Process();
   p.StartInfo = psi;
   p.Start();
   CloseAppplication();
}

Hope this helps. Good luck!

Audie