Пример #1
0
        /// <summary>
        ///     Iterates through the open processes list and finds the procs based on the given parameters.
        /// </summary>
        /// <param name="method">The different methods you can use to find a process.</param>
        /// <param name="value">The value that must fit into the method's object that can be found in the doc.</param>
        /// <returns>The processes in array or an empty array</returns>
        /// <exception cref="ArgumentException">Incorrect type of <see cref="object"/> was passed.</exception>
        public static Process[] Find(ProcessSearchMethod method, object value) {
            var procs = new Process[0];
            switch (method) {
                case ProcessSearchMethod.TitleName:
                    if (value is string == false) throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                    return Process.GetProcesses().Where(p => p.MainWindowTitle.ToLowerInvariant().Contains((value as string).ToLowerInvariant())).ToArray();
                case ProcessSearchMethod.ProcessName:
                    if (value is string == false) throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                    var s = value as string;
                    return Process.GetProcesses().Where(p => p.ProcessName.Contains(s.Contains(".") ? s.Split('.')[0] : s)).ToArray();
                case ProcessSearchMethod.Handle:
                    if (value is IntPtr == false) throw new ArgumentException("Incorrect value was passed compared to the method ("+method+")");
                    var ptr = (IntPtr) value;
                    return Process.GetProcesses().Where(p => {
                                                            try {
                                                                return p.Handle.Equals(ptr);
                                                            }
                                                            catch { return false;}
                                                        }).ToArray();
                case ProcessSearchMethod.WindowHandle:
                    if (value is IntPtr == false) throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                    var ptrr = (IntPtr)value;
                    return Process.GetProcesses().Where(p =>
                    {
                        try
                        {
                            return p.MainWindowHandle.Equals(ptrr);
                        }
                        catch { return false; }
                    }).ToArray();
                case ProcessSearchMethod.ProcessInfo:
                    if (value is ProcessInfo == false) throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                    var pi = value as ProcessInfo;
                    return Find(ProcessSearchMethod.ProcessName, pi.Name);
                case ProcessSearchMethod.Process:
                    if (value is Process == false) throw new ArgumentException("Incorrect value was passed compared to the method ("+method+")");
                    var process = (Process)value;
                    return Process.GetProcesses().Where(p => p.Id == process.Id && p.ProcessName == process.ProcessName && p.MachineName == process.MachineName).ToArray(); 

                default:
                    throw new ArgumentOutOfRangeException("method");
            }
        }
Пример #2
0
        /// <summary>
        ///     Iterates through the open processes list and finds the procs based on the given parameters.
        /// </summary>
        /// <param name="method">The different methods you can use to find a process.</param>
        /// <param name="value">The value that must fit into the method's object that can be found in the doc.</param>
        /// <returns>The processes in array or an empty array</returns>
        /// <exception cref="ArgumentException">Incorrect type of <see cref="object"/> was passed.</exception>
        internal static Process[] Find(ProcessSearchMethod method, object value)
        {
            var procs = new Process[0];

            switch (method)
            {
            case ProcessSearchMethod.TitleName:
                if (value is string == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                return(Process.GetProcesses().Where(p => p.MainWindowTitle.ToLowerInvariant().Contains((value as string).ToLowerInvariant())).ToArray());

            case ProcessSearchMethod.ProcessName:
                if (value is string == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                var s = value as string;
                return(Process.GetProcesses().Where(p => p.ProcessName.Contains(s.Contains(".") ? s.Split('.')[0] : s)).ToArray());

            case ProcessSearchMethod.Handle:
                if (value is IntPtr == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                var ptr = (IntPtr)value;
                return(Process.GetProcesses().Where(p => {
                    try {
                        return p.Handle.Equals(ptr);
                    }
                    catch { return false; }
                }).ToArray());

            case ProcessSearchMethod.WindowHandle:
                if (value is IntPtr == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                var ptrr = (IntPtr)value;
                return(Process.GetProcesses().Where(p =>
                {
                    try
                    {
                        return p.MainWindowHandle.Equals(ptrr);
                    }
                    catch { return false; }
                }).ToArray());

            case ProcessSearchMethod.ProcessInfo:
                if (value is ProcessInfo == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                var pi = value as ProcessInfo;
                return(Find(ProcessSearchMethod.ProcessName, pi.Name));

            case ProcessSearchMethod.Process:
                if (value is Process == false)
                {
                    throw new ArgumentException("Incorrect value was passed compared to the method (" + method + ")");
                }
                var process = (Process)value;
                return(Process.GetProcesses().Where(p => p.Id == process.Id && p.ProcessName == process.ProcessName && p.MachineName == process.MachineName).ToArray());

            default:
                throw new ArgumentOutOfRangeException("method");
            }
        }
Пример #3
0
 /// <summary>
 ///     Iterates through the list of processes and if any fit the parameters, it returns true.
 /// </summary>
 /// <param name="method">The different methods you can use to find a process.</param>
 /// <param name="value">The value that must fit into the method's object that can be found in the doc.</param>
 /// <returns>The process exists</returns>
 /// <exception cref="ArgumentException">Incorrect type of <see cref="object"/> was passed.</exception>
 internal static int Exists(ProcessSearchMethod method, object value)
 {
     return(Find(method, value).Length);
 }
Пример #4
0
 /// <summary>
 ///     Iterates through the list of processes and if any fit the parameters, it returns true.
 /// </summary>
 /// <param name="method">The different methods you can use to find a process.</param>
 /// <param name="value">The value that must fit into the method's object that can be found in the doc.</param>
 /// <returns>The process exists</returns>
 /// <exception cref="ArgumentException">Incorrect type of <see cref="object"/> was passed.</exception>
 public static int Exists(ProcessSearchMethod method, object value) {
     return Find(method, value).Length;
 }