View Single Post
 
Old July 28th, 2003, 09:20 PM
mbarrett mbarrett is offline
Registered User
 
Join Date: Jul 2003
Location: Lafayette, in, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the complete test app.
The program works if you remove "[STAThread]"
from the code. What does "[STAThread]" do?


using System;
using System.Management;
using System.Collections;

namespace ConnectErr
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
             //[STAThread] * remove and code works. Why ????
        static void Main(string[] args)
        {
            string[] systems = {"cpu1", "cpu1", "OnLine-NotResponding", "bogusname"};
            ConnectionOptions wmiConnOption;
            ManagementObjectSearcher osSearch;
            ManagementObjectCollection osInfo;
            ManagementScope scope;
            ObjectQuery osQuery;
            wmiConnOption = new ConnectionOptions();
            wmiConnOption.Impersonation = ImpersonationLevel.Impersonate;
            osQuery = new ObjectQuery("Select * from Win32_OperatingSystem");
            for (int i = 0; i < systems.Length; i++)
            {
                scope = new ManagementScope("\\\\" + systems[i] + "\\root\\cimv2", wmiConnOption);
                osSearch = new ManagementObjectSearcher(scope, osQuery);
                try
                {
                    scope.Connect();
                }
                catch(System.Runtime.InteropServices.COMException e)
                {
                    Console.WriteLine("Access error: " + e.Source);
                }
                if(scope.IsConnected)
                {
                    osInfo = osSearch.Get();
                    foreach(ManagementObject osItem in osInfo)
                    {
                        Console.WriteLine(systems[i] + " OS Version: " + osItem["Version"]);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}


- Mike