0

My account has administrative privileges. I access WMI on a Windows 7 Enterprise VM with powershell as follows:

 Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct  -ComputerName $computername

and with C# as follows:

        string computer = Environment.MachineName;
        string wmipath = @"\\" + computer + @"\root\SecurityCenter2";
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath,
              "SELECT * FROM AntivirusProduct");
            ManagementObjectCollection instances = searcher.Get();
            //MessageBox.Show(instances.Count.ToString()); 
            foreach (ManagementObject queryObj in instances)
            {
                return queryObj[type].ToString();
            }
        }

        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

However, the code in Powershell works always but the code in C# works only if I run the program as administrator explicitly. Can I add anything to the C# code so that it could run for a user with administrative right without starting the C# program explicitly as administrator?

Greg
  • 1,227
  • 5
  • 23
  • 52
  • No you can't. With UAC activated in W7, your "administrative priv." will only let you answer YES on the UAC popup when permissions are needed. If your app needs admin-rights you need to start the application with "run as administrator" or build in a check in your software that will trigger the UAC-popup. Remember that you can go into properties on a shortcut -> Shortcut -> Advances -> always admin to always run as admin. Last option is ofc. to deactivate UAC, but that's not popular when it's work-related :) Please correct me if I'm wrong. – Frode F. Dec 17 '12 at 17:21
  • funny thing is, on one of another machine the same code doesn't demand being run as administrator, I'm slightly confused.... – Greg Dec 20 '12 at 09:25

2 Answers2

0

You can force the UAC prompt without having to explicitly "Run as administrator" by editing the manifest (an XML file that goes in the same directory as your C# executable).

See the StackOverflow answers for "How to force my .NET App to run as administrator on Windows 7?".

Community
  • 1
  • 1
Daryn
  • 4,791
  • 4
  • 39
  • 52
0

Usually when my application can be run only from machine administrator I use this method to verify admin right:

public static bool HasAdministrativeRight()
        {
            WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

In the main part of my code (form or console application)

        if (!HasAdministrativeRight()) 
        {
          if (RunElevated(""))                
          {
           Application.Exit();
          }
        }

Code for run in elevated way:

private static bool RunElevated(string args)
        {
            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.Verb = "runas";
            processInfo.FileName = Application.ExecutablePath;
            processInfo.Arguments = args;
            try
            {
                Process.Start(processInfo);
                return true;
            }
            catch (Exception)
            {
                //Do nothing. Probably the user canceled the UAC window
            }
            return false;
        }
CB.
  • 58,865
  • 9
  • 159
  • 159