473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Process.GetProcesses(userName) throws InvalidOperationException

Hello all. I came across a posting that addressed exactly what I'm trying to accomplish, i.e. "to get a list of Processes that have a window" by userName, which I found at http://bytes.com/topic/c-sharp/answe...if-app-running.

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
  1. string userName = Environment.UserName;
  2. Process[] procList = Process.GetProcesses(userName);
  3. for (int i = 0; i < procList.Length; i++)
  4. {
  5.     if (procList[i].MainWindowHandle != IntPtr.Zero)
  6.     {
  7.         listBox1.Items.Add(procList[i].ProcessName + "\t" +
  8.             procList[i].MainWindowTitle);
  9.     }
  10. }
Note: in testing the code I replaced ArrayList with a simple winform listbox, although the final code will be placed in a Form_Deactivate event to run automatically.

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
Jun 13 '14 #1

✓ answered by Luk3r

This is a little sloppy since I'm not a C# developer, but I actually use something very similar to this in VB.NET so I just mildly converted it for you. I tested it and it works fine.

Expand|Select|Wrap|Line Numbers
  1.         {
  2.             Process[] myProcesses = Process.GetProcesses();
  3.  
  4.             foreach (Process processWithWindow in myProcesses)
  5.             {
  6.                 if (processWithWindow.MainWindowTitle != "")
  7.                 {
  8.                     listBox1.Items.Add(processWithWindow.MainWindowTitle + "  -  " + processWithWindow.ProcessName);
  9.                 }
  10.             }
  11.         }

9 5644
Luk3r
300 256MB
This is a little sloppy since I'm not a C# developer, but I actually use something very similar to this in VB.NET so I just mildly converted it for you. I tested it and it works fine.

Expand|Select|Wrap|Line Numbers
  1.         {
  2.             Process[] myProcesses = Process.GetProcesses();
  3.  
  4.             foreach (Process processWithWindow in myProcesses)
  5.             {
  6.                 if (processWithWindow.MainWindowTitle != "")
  7.                 {
  8.                     listBox1.Items.Add(processWithWindow.MainWindowTitle + "  -  " + processWithWindow.ProcessName);
  9.                 }
  10.             }
  11.         }
Jun 13 '14 #2
Hello Luk3r and thanks for your reply. It does give me a list of running processes having a window without throwing the aforementioned exceptions.

Curiously though, it did not show a Windows Explorer window that i had open, whereas TaskManager does show that window along with the others.

Not sure why it excluded that window. Any ideas why that window wouldn't show up in the list?
Thanks.
Jun 13 '14 #3
Luk3r
300 256MB
Excellent question. My best guess is: Likely because Windows Explorer is launched from the explorer.exe process rather than running a new process. I find it hard to imagine you'd ever want to kill the "explorer.exe" process to close the Windows Explorer window :)
Jun 13 '14 #4
Hello Luk3r, you are correct, I wouldn't want to kill explorer.exe, so that window wouldn't be a problem.

Interestingly though, the Win32Exception returned, with an exception message of "Access is denied" and this is on my own system, which has Administrator rights.

I can't imagine that I would need elevated rights on my own box.
Any thoughts on why I'm getting this, or perhaps more importantly, how to get around this exception, as my application is going to be ported over to another person's Windows 7 machine?

Thanks again.
Jun 13 '14 #5
Also, to note, this Win32Exception occurred when I attempted the
Expand|Select|Wrap|Line Numbers
  1. process.Kill() 
Jun 13 '14 #6
Luk3r
300 256MB
Can you please post your new code (the code including the process.Kill)?
Jun 13 '14 #7
Hello Luk3r, sorry I didn't post this sooner. It's entirely possible that I don't have the syntax correct on this Kill function. I've used a process.Kill before when I was looking to stop one specific process and it worked fine.

However, what I'm trying to do here is kill any process with a window that doesn't correspond to my running application.

When I debugged my code, I found that the list of processes being returned from procList had every process on the system, not just those with a window, i.e. background processes as well, which I do not want to kill.

Here is a sample of my code, where I first excluded the processes of my application in a "switch" statement, then in the default statement I want to do the Kill.

Expand|Select|Wrap|Line Numbers
  1. public void getProcesses()
  2. {
  3.     try
  4.     {
  5.         Process[] procList = Process.GetProcesses();
  6.  
  7.         foreach (Process process in procList)
  8.         {
  9.             switch (process.MainWindowTitle + " - " + process.ProcessName)
  10.             {
  11.                 case "Specific  App in VS - Microsoft Visual Studio - devenv":
  12.                 case "Specific  App (Running) - Microsoft Visual Studio - devenv":
  13.         case "Specific App already deployed - The Specific App name":
  14.                     break;
  15.                 default:
  16.                       process.Kill(); //anything other than the currently running Specific App
  17.                       break;
  18.             }
  19.         }
  20.     }
  21.     catch (Win32Exception) { }
  22.     catch (NotSupportedException) { }
  23.     catch (InvalidOperationException) { }
  24. }
Jun 13 '14 #8
Hello Luk3r, well, as I sometimes like to say, "the littlest things can cause the biggest problems." I had originally created a mockup using your code suggestion, using a simple form with a list-box and button, which worked great.

I then attempted to port this over to my main body of code in the real application and completely forgot to include the check if the MainWindowTitle was empty and wondered why procList had every background process in the system and not just those with a window. Oh well. :-(

When I included that line in my code, it works fine. (scrape egg off face here). Therefore, your code suggestion solved the problem I was working on and I give you a big Kudos for thinking to check the MainWindowTitle.

Many thanks for your help.
Jun 14 '14 #9
Luk3r
300 256MB
Sorry for the late reply. Busy weekend! Glad you got it all figured out and happy coding! :)
Jun 16 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Raj | last post by:
Hi! Every time my applicaiton call "System.Diagnosis.Process.GetProcesses() and some other related APIs in dotnet framework, it throws the following exception: "An unhandled exception of type...
0
by: Dan McGuffin | last post by:
I am trying to develop some vb.net code that will will store 1 or more processes in an array of processes, then loop through the array determining when each one has exited. Upon each one exiting, I...
2
by: RickN | last post by:
I am running Visual Studio 2003 via Terminal Services on Windows Server 2003. I connect to the server as Admin. Everything works fine until I try to build an app (or even use the MSDN sample code)...
2
by: Fei Yuan | last post by:
Please forgive me re-posting this question since I wasn't clear in my original post. ---- Starting an external process needs to pass it a ProcessStartInfo() object. ProcessStartInfo has a...
4
by: web1110 | last post by:
Given a Process, is there a way to get the full path name of the executable?
0
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then, Process.Start throws an Access is Denied Exception. This is with .NET 2.0, of course...
5
by: Vijaya P Krishna | last post by:
Hi, I have a .NET Windows Forms application, written in VB.NET and C#. I am opening a URL from the application using Process.Start(). The URL points to a java servlet running on apache-tomcat....
3
by: Joseph Geretz | last post by:
System.InvalidOperationException: WebServiceBindingAttribute is required on proxy classes. My environment: Visual Studio 2005, targeting FX 2.0; I've developed a Web Service which uses DIME to...
7
by: =?Utf-8?B?ams=?= | last post by:
I am using System.Diagnostics.Process class to open a word document by call ing Process.Start("test.doc"). I am using C# as programming language. On some of the computers on running this code i get...
1
by: JSha | last post by:
Hello, The application I am using works on most machines except one where in it throws the following exception... Error: Process Performance Counter is Disabled, so requested operation cannot...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.