示例#1
0
        private void RunTheMethod()
        {
            if (WmiObject == null || WmiMethod == null)
            {
                return;
            }
            //var watcher = new ManagementOperationObserver();
            var inParams = WmiMethod.InParameters;
            var options  = new InvokeMethodOptions();
            ManagementBaseObject result = null;

            bRun.Enabled = false;
            try
            {
                result = WmiObject.InvokeMethod(WmiMethod.Name, inParams, options);
            }
            catch (ManagementException ex)
            {
                ShowFAIL(ex.Message);
            }
            bRun.Enabled = true;
            if (result == null)
            {
                return;
            }
            LB.Items.Add(string.Empty);
            string str;

            foreach (PropertyData pd in result.Properties)
            {
                str = String.Format(CultureInfo.InvariantCulture, "{0} = {1}", pd.Name, pd.Value);
                LB.Items.Add(str);
            }
            var resultProp = new PropertyDataExt(result.Properties[m_ReturnValueName]);

            if (resultProp.Value != null)
            {
                var value   = (int)((UInt32)resultProp.Value);
                var message = new Win32Exception(value).Message;
                str = String.Format(CultureInfo.InvariantCulture, METHOD_FAIL_FMT, value, message);
                LB.Items.Add(str);
                if (value == 0)
                {
                    ShowOK(Resources.MethodForm_Success);
                    if (!m_OutParamPresent)
                    {
                        timerOK.Enabled = true;
                    }
                }
                else
                {
                    ShowFAIL(String.Format(CultureInfo.InvariantCulture, METHOD_FAIL_FMT, value, message));
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                string          ComputerName = "localhost";
                ManagementScope Scope;

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope          = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                {
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
                }

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT Handle FROM Win32_Process Where Name='notepad.exe'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    WmiObject.InvokeMethod("Terminate", null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }