使用C#中的WMI获取Windows Server状态



我需要服务在Windows Server上自动启动。有人可以向我展示一种使用WMI获得Windows Server(在线或离线)状态的方法。

预先感谢您

请尝试以下代码,

string FullComputerName = "<Name of Remote Computer>";
            ConnectionOptions options = new ConnectionOptions();
            ManagementScope scope = new ManagementScope("\\" + FullComputerName + "\root\cimv2", options);
            scope.Connect();
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalService");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject queryObj in queryCollection)
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_TerminalService instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Started: {0}", queryObj["Started"]);
                Console.WriteLine("State: {0}", queryObj["State"]);
                Console.WriteLine("Status: {0}", queryObj["Status"]);
            }

来源:-https://social.msdn.microsoft.com/forums/vstudio/en-us/a25d3071-2283-41c6-9262-6862-6860d796596596363/使用-C?forum = csharpgeneral

最新更新