RPS.Screensaver.singleProcess C# (CSharp) Method

singleProcess() private static method

private static singleProcess ( Actions action ) : bool
action Actions
return bool
        private static bool singleProcess(Actions action) {
            Process currentProcess = Process.GetCurrentProcess();
            // Adapted from: http://stackoverflow.com/questions/504208/how-to-read-command-line-arguments-of-another-process-in-c
            string wmiQuery = string.Format("select Handle, ProcessId, CommandLine from Win32_Process where Name='{0}'", Path.GetFileName(currentProcess.MainModule.FileName));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
            ManagementObjectCollection retObjectCollection = searcher.Get();
            foreach (ManagementObject retObject in retObjectCollection) {
                if (currentProcess.Id != Convert.ToInt32(retObject["ProcessId"])) {
                    //Console.WriteLine("[{0}]", retObject["CommandLine"] + " " + retObject["ProcessId"] + " ~ " + currentProcess.Id);
                    // If both self and process as screensaver /s switch to existing and exit
                    if (!System.Diagnostics.Debugger.IsAttached) {
                        if (action == Actions.Screensaver && !retObject["CommandLine"].ToString().Contains("/c") && !retObject["CommandLine"].ToString().Contains("/p")) {
                            try {
                                Process p = Process.GetProcessById(Convert.ToInt32(retObject["ProcessId"]));
                                SwitchToThisWindow(p.MainWindowHandle, false);
                                Environment.Exit(0);
                            } catch (System.ArgumentException ae) {

                            }
                        }
                        // Make this version read only
                        return true;
                    }
                }
            }       
            return false;
        }