2

I have a C# application that can be run by a argument or not. For example

  • C:\MyApplication.exe
  • C:\MyApplication.exe -record

Both of these instances can run at the same time. Inside another application I need to know if any instances that is started with parameter runs and if so I would like to kill them.

I tried to get it by such a code:

Process.GetProcesses().Where(x =>
               x.ProcessName.StartsWith("MyApplication") 
           && (x.StartInfo.Arguments == "-record"));

However StartInfo.Arguments is empty even if I set it manually when process started with the argument. How can I find my processes that started with argument?

cuongle
  • 74,024
  • 28
  • 151
  • 206
Demir
  • 1,787
  • 1
  • 29
  • 42
  • You should set up a private mechanism for your application to signal whether it wants to be killed or not. Command line arguments are not reliable. – Raymond Chen Oct 17 '12 at 18:27
  • 1
    Killing is not the main point. What important to me is finding my processes that started with a command line argument. – Demir Oct 17 '12 at 18:34
  • Once you find them, you'll need to communicate with them. You may as well have the "are you in recording mode?" question be part of that communication. – Raymond Chen Oct 17 '12 at 20:15
  • It might not be a good idea to kill you processes externally. I would consider a design that would use a Windows named event to tell the processes to cleanly close themselves. – Ran Oct 17 '12 at 18:27
  • main point is finding my processes that started with a command line argument. You can ignore killing part in the question. – Demir Oct 17 '12 at 18:36

0 Answers0