wmic via .NET or C#
All,
Please forgive my ignorance of C#/.NET, I am absolutely new to both (mostly Java, C/C++) and find myself tasked with creating some code that performs the same thing as the "wmic qfe" and "wmic os" commands available at the DOS prompt.
Can this be done? If so, any help is appreciated. 开发者_如何学编程 I am trying to working my way through the System.Management.Instrumentation namespace since wmic is an acronym for Windows Management Instrumentation Command (according to Google), but have as yet to discover anything useful.
Thanks in advance, Toddw
Instead of using the Management class, you can simply invoke the wmic.exe which is a WMI console as you can figure out from the exe name itself. Here is the code that I used to open notepad on a remote computer using WMIC invoked through C#.net. Try it out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace RemoteProcessDemo
{
class Program
{
static void Main(string[] args)
{
string strComputerName = "";
string strProcString = "";
try
{
strComputerName = args[0];
Process.Start("wmic", "/node:" + strComputerName + " process call create notepad.exe");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Regards, Tushar
You will indeed need to use the System.Management namespace to perform WMI queries. There is lots info on using WMI from C#, Microsoft's is (archived) here
For your specific cases:
qfe - query the Win32_QuickFixEngineering class
os - query the Win32_OperatingSystem class
精彩评论