2

I am using the following code to find a list of Devices attached to COM Port.

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");

With Windows XP, the code works fine, but with Windows-7, I get an Access Denied Exception. I am running under full administrative privilege in both the cases, then why would I face this error with Windows 7 and not XP.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • Are you using this code in a web project? – Kamyar Apr 18 '11 at 09:05
  • 1
    Are you really running under full administrative privilege (after accepting the UAC prompt), or just with an administrator user account? – Adrian Cox Apr 18 '11 at 09:11
  • @Adrian:positively Full administrative. – Shamim Hafiz - MSFT Apr 18 '11 at 09:18
  • @Gunner in that case putting a manifest on it won't help. I never completely solved my version of this problem: http://stackoverflow.com/questions/2279646/finding-usb-serial-ports-from-a-net-application-under-windows-7 – Adrian Cox Apr 18 '11 at 09:25
  • @Adrian: Good to know that you and I are facing the same problem :), but also feels bad that there appears to be no Generic Solution :(. Could you tell why it doesn't work with Windows 7 but does with XP. – Shamim Hafiz - MSFT Apr 18 '11 at 09:32
  • @Gunner: I didn't investigate much deeper, as my code needed to run as a standard user under Windows 7. There's another list of serial ports in `System.IO.Ports.SerialPort.GetPortNames()`, but it didn't have enough detail for me. – Adrian Cox Apr 18 '11 at 09:34
  • @Gunner: let's get clarity in this discussion. Are you tried to right click on your exe and choosing 'Run as Administaror'? – Anton Semenov Apr 18 '11 at 09:37
  • @Anton: No, I logged in with Admin account and directly installed. I was given the warning that this installation is trying to make changes to the computer, I clicked yes and installation finished well. – Shamim Hafiz - MSFT Apr 18 '11 at 09:56
  • 1
    @Gunner: Hmm, In win7 when you are running app under Administrator account its not have full administrative rights by default, to obtain full rights you should right click on exe and choose 'Run as Administaror' after that you should click yes in prompt dialog. The warning about changes in the system is not a garantee that you have all sufficient rights – Anton Semenov Apr 18 '11 at 11:18
  • @Anton: Thanks. This has taken me a step forward. Though my issue isn't fully resolved, but I have overcome one hurdle. – Shamim Hafiz - MSFT Apr 18 '11 at 11:33

2 Answers2

1

I just ran your code under admin in my win7 rig and it worked properly while it failed within user context. Your process is running under UAC even though you yourself is logged in as admin. You need to secure elevated privileges for the application.

mmix
  • 6,057
  • 3
  • 39
  • 65
  • Thanks for the post. Could you please provide some resource on how I can elevate the privileges for the application. Thanks. – Shamim Hafiz - MSFT Apr 18 '11 at 11:16
  • There are several ways, one is to include a manifest file requesting elevated privileges for an exe. See here: http://blogs.msdn.com/b/heaths/archive/2006/09/26/custom-actions-under-uac-in-vista.aspx – mmix Apr 18 '11 at 11:25
1

You need to elevate privileges of your app: See this Q&A

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433