Ejemplo n.º 1
0
        public static ManagementObjectCollection QueryLocalWMICollection(string scope, string query)
        {
            ManagementScope ms        = null;
            int             numTrial  = 0;
            bool            connected = false;

            do
            {
                try
                {
                    if (scope == null)
                    {
                        ms = new ManagementScope(@"\\.\root\cimv2", new ConnectionOptions());
                    }
                    else
                    {
                        ms = new ManagementScope(scope, new ConnectionOptions());
                    }

                    ms.Connect();
                    connected = true;
                }
                catch (Exception) // in the booting period, the WMI service may not be ready;
                {
                    EucaLogger.Warning("WMI service is not responding; will retry");
                    System.Threading.Thread.Sleep(PAUSE_SEC_BETWEEN_RETRY * 1000);
                    continue;
                }
            } while (numTrial++ < RETRY);

            if (!connected || ms == null || !ms.IsConnected)
            {
                throw new EucaException("Cannot establish connection to the management provider");
            }

            ManagementObjectSearcher   searcher = new ManagementObjectSearcher(ms, new ObjectQuery(query));
            ManagementObjectCollection result   = searcher.Get();

            searcher.Dispose();
            return(result);
        }
Ejemplo n.º 2
0
        public static System.Diagnostics.Process SpawnProcess(string exe, string args, string workingDir,
                                                              KeyValuePair <string, string>[] env, DataReceivedEventHandler outputHandler,
                                                              DataReceivedEventHandler errorHandler)
        {
            try
            {
                System.Diagnostics.Process proc = new System.Diagnostics.Process();

                proc.StartInfo.UseShellExecute = false;
                if (workingDir != null)
                {
                    proc.StartInfo.WorkingDirectory = workingDir;
                }
                proc.StartInfo.RedirectStandardError  = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.FileName  = exe;
                proc.StartInfo.Arguments = args;
                if (env != null)
                {
                    foreach (KeyValuePair <string, string> kv in env)
                    {
                        if (kv.Key.ToLower() == "path")
                        {
                            try
                            {
                                string path = proc.StartInfo.EnvironmentVariables["Path"];
                                if (path != null)
                                {
                                    path += string.Format(";{0}", kv.Value);
                                }
                                else
                                {
                                    path = kv.Value;
                                }
                                proc.StartInfo.EnvironmentVariables["Path"] = path;
                            }
                            catch (Exception e)
                            {
                                EucaLogger.Exception(EucaLogger.LOG_WARNING, "couldn't set path variable", e);
                            }
                        }
                        else
                        {
                            proc.StartInfo.EnvironmentVariables.Add(kv.Key, kv.Value);
                        }
                    }
                }
                if (outputHandler != null)
                {
                    proc.OutputDataReceived += outputHandler;
                }
                if (errorHandler != null)
                {
                    proc.ErrorDataReceived += errorHandler;
                }

                proc.Start();
                return(proc);
            }
            catch (Exception e)
            {
                throw new EucaException("Execution failed: " + e.Message, e);
            }
        }