Sam HobbsRank #107 · 20.5K reputationPosted Dec 15, 2011, 10:55 AM EST
0
Welcome to C# Corner.
You can list the hotfixes installed in a computer using WMI. You can use the .Net System.Management Namespace to use WMI; look in this web site for articles about using WMI. The following is VBscript code that does what you need; I hope this is enough for you to be able to determine how to do it using C#.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colQuickFixes = objWMIService.ExecQuery _ ("Select * from Win32_QuickFixEngineering") For Each objQuickFix in colQuickFixes Wscript.Echo "Description: " _ & objQuickFix.Description Wscript.Echo "Hotfix ID: " & objQuickFix.HotFixID Next
nilesh phule7 reputationPosted Dec 15, 2011, 9:36 PM EST
0
Thanks Sam,but its better if i can get c# code as i am not familiar with VBScript
0
Sam HobbsRank #107 · 20.5K reputationPosted Dec 15, 2011, 11:22 PM EST
0
You can do it if you try.
How about we do this. If you read the articles in this web site about the classes in the System.Management Namespace for using WMI then I or someone will fill in the parts that you need help with. Please don't avoid trying just because you assume you cannot do it.
0
VulpesRank #55 · 29.2K reputationPosted Dec 16, 2011, 3:39 AM EST
0
Although Sam's VBScript code works, it's unlikely that you'll be able to convert it to C# as it stands because of the connection string used. I got an 'invalid parameter error' when I tried it and, frankly, I've no idea why :(
However, if you don't mind waiting a few seconds for the output, this works fine on my machine:
using System;
using System.Management;
class Program
{
static void Main()
{
string computer = ".";
ManagementScope scope = new ManagementScope(@"\\" + computer + @"\root\cimv2");
ManagementObjectSearcher quickFixes = new ManagementObjectSearcher(scope, new ObjectQuery("Select * from Win32_QuickFixEngineering"));
foreach(ManagementObject quickFix in quickFixes.Get())
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2011, 8:47 AM EST
0
I see absolutely no reason why someone would have difficulty knowing how to put the Win32_QuickFixEngineering WMI class into code that uses another WMI class. The only part of that code that needs to be converted is to use the Win32_QuickFixEngineering class in C# code that uses the Management namespace classes for WMI. I said I would help if Nilesh were to read the articles. Do you disagree that reading the articles is a good thing to do?
0
VulpesRank #55 · 29.2K reputationPosted Dec 16, 2011, 11:28 AM EST
0
I hope nilesh does read the articles, as we have some good ones.
However, I don't think they'll tell him why the VBScript connection string doesn't work in C#. There's nothing worse when you're trying to learn new stuff than to be faced with such mysteries which is why I posted.
0
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2011, 12:10 PM EST
0
I said I would answer specific questions, so if the response would have been more specific than "if i can get c# code" then I would have answered that with something specific.
So I should have just said that the Win32_QuickFixEngineering class could be used. In this case, less is more.
Probably the problem you encountered with the VBScript code is the continuation. The following is a modified version.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering") For Each objQuickFix in colQuickFixes Wscript.Echo "Description: " & objQuickFix.Description Wscript.Echo "Hotfix ID: " & objQuickFix.HotFixID Next
0
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 3:48 AM EST
0
The VBScript works fine.
The problem is if I try to graft the connection string, as it stands, into the C# code I posted earlier:
ManagementScope scope = new ManagementScope(@"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2");
It compiles OK but when I run it (Windows 7) I get:
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..ctor(String path)
at System.Management.ManagementScope..ctor(String path)
at Program.Main()
However, if you drop this bit:
winmgmts:{impersonationLevel=impersonate}!
which is probably what you'd do if you hadn't seen the VBScript code, then it again works fine.
So far I haven't been able to come up with an explanation for this :-/
0
Sam HobbsRank #107 · 20.5K reputationPosted Dec 17, 2011, 11:07 PM EST
0
I am sorry that posting the script was such a problem. I did not expect it to be a problem.
A WMI connection string does have multiple parts. I do not understand all the parts. As best as I know, the .Net Management classes do not use all the parts; it might be that some of the parts are implied and never specified by us and never shown to us.
A WMI connection string will be in the format used in the script for any unmanaged code; whether the code is VBScript, JavaScript, C++ or any unmanaged code. It is not the language that has any relevant affect whatsoever; it is that the .Net Management classes simplifies things, which can make things very confusing without an explanation, and it is typical for Microsoft to not explain things like that.
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 15, 2011, 5:32 AM EST
Please refer the below link
http://www.ehow.com/how_5229661_install-microsoft-hotfixes.html
Thanks
Sam HobbsRank #107 · 20.5K reputationPosted Dec 15, 2011, 10:55 AM EST
You can list the hotfixes installed in a computer using WMI. You can use the .Net System.Management Namespace to use WMI; look in this web site for articles about using WMI. The following is VBscript code that does what you need; I hope this is enough for you to be able to determine how to do it using C#.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery _
("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colQuickFixes
Wscript.Echo "Description: " _
& objQuickFix.Description
Wscript.Echo "Hotfix ID: " & objQuickFix.HotFixID
Next
nilesh phule7 reputationPosted Dec 15, 2011, 9:36 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 15, 2011, 11:22 PM EST
How about we do this. If you read the articles in this web site about the classes in the System.Management Namespace for using WMI then I or someone will fill in the parts that you need help with. Please don't avoid trying just because you assume you cannot do it.
VulpesRank #55 · 29.2K reputationPosted Dec 16, 2011, 3:39 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2011, 8:47 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 16, 2011, 11:28 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2011, 12:10 PM EST
So I should have just said that the Win32_QuickFixEngineering class could be used. In this case, less is more.
Probably the problem you encountered with the VBScript code is the continuation. The following is a modified version.
Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colQuickFixes
Wscript.Echo "Description: " & objQuickFix.Description
Wscript.Echo "Hotfix ID: " & objQuickFix.HotFixID
Next
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 3:48 AM EST
The problem is if I try to graft the connection string, as it stands, into the C# code I posted earlier:
It compiles OK but when I run it (Windows 7) I get:
Sam HobbsRank #107 · 20.5K reputationPosted Dec 17, 2011, 11:07 PM EST
A WMI connection string does have multiple parts. I do not understand all the parts. As best as I know, the .Net Management classes do not use all the parts; it might be that some of the parts are implied and never specified by us and never shown to us.
A WMI connection string will be in the format used in the script for any unmanaged code; whether the code is VBScript, JavaScript, C++ or any unmanaged code. It is not the language that has any relevant affect whatsoever; it is that the .Net Management classes simplifies things, which can make things very confusing without an explanation, and it is typical for Microsoft to not explain things like that.
nilesh phule7 reputationPosted Dec 20, 2011, 10:39 PM EST
Posted Dec 1, 2017, 1:26 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts