I'm writing this in C-Sharp in Visual Studio 2010 and am testing it on a Windows Vista 32-bit system, but the final target will be a Windows 7 64-bit system.
I want to get a list of running processes that have a window, i.e. what would show up in TaskManager and that are started by the user. My intention is to do a process.Kill() on certain processes.
The code example shown in the URL above suggested acquiring the local users userName from as follows:
Expand|Select|Wrap|Line Numbers
- string userName = Environment.UserName;
- Process[] procList = Process.GetProcesses(userName);
- for (int i = 0; i < procList.Length; i++)
- {
- if (procList[i].MainWindowHandle != IntPtr.Zero)
- {
- listBox1.Items.Add(procList[i].ProcessName + "\t" +
- procList[i].MainWindowTitle);
- }
- }
While userName returns the current user name, the problem I've experienced is that plugging in userName to get the processes throws an InvalidOperationException with the additional details of "Couldn't connect to remote machine".
Please note that this is a standalone computer not connected to any network and neither will the Windows 7 computer be on any network.
I read comments on another forum indicating that there are 2-flavors of Process.GetProcesses and to leave out the userName. However, trying that not only pulls back all the running processes on the system, those with a window and those that do not; but then it also throws a Win32Exception.
I want to get only those processes started by the user which have a window. Does anyone know how to get around the InvalidOperationException being thrown? The example in the aforementioned URL appears to be what I'm after, but won't run without this error. Thank you