using System; using System.Diagnostics; using System.Linq; namespace VDash.WindowSelectors { public class FromProcessName : IWindowSelector { string m_ProcName = ""; public FromProcessName(string procName) { if (string.IsNullOrWhiteSpace(procName)) { throw new ArgumentNullException("procName", "procName must not be null, whitespace, or empty."); } m_ProcName = procName; } public Process[] GetProcesses() { return Process.GetProcessesByName(m_ProcName); } public Process GetProcess() { var procs = GetProcesses(); if (procs.Count() <= 0) { throw new WindowNotFoundException($"No process found with name {m_ProcName}."); } return procs.First(); } public IntPtr GetWindowHandle() { return GetProcess().MainWindowHandle; } public WindowInfo GetWindowInfo() { var ret = WindowInfo.GetWindowList().SingleOrDefault(x => x.hWnd == GetWindowHandle()); if(ret == null) { throw new WindowNotFoundException($"Unable to locate window for process {m_ProcName}."); } return ret; } } }