locked
WMI: Retrieving Antivirus product info with WMI and C#? I Need help can't find the solution. RRS feed

  • Question

  • Hi!

    I have been searching for a solution a long time now. I know that I've done something wrong in this code but I can't find the solution, it's of course a ManagementException which appears for me. I need this or a type of code example which, shows how to do?


     private void Form1_Load(object sender, EventArgs e)  
            {  
                label1.Text = "" + Antivirus("companyName");  
                label2.Text = "" + Antivirus("displayName");     
            }  
            private string Antivirus(string type)  
            {  
                string computer = ".";  
                string wmipath = "winmgmts:{impersonationLevel=impersonate}!\\" + computer + "\\root\\SecurityCenter";  
                //ManagementPath path = new ManagementPath(wmipath);  
                //ManagementObject obj = new ManagementObject(path);  
                string oo = null;   
                ManagementObjectSearcher search = new ManagementObjectSearcher(wmipath + "SELECT * FROM AntivirusProduct");  
                foreach (ManagementObject o in search.Get())  
                {  
                    oo = o[type].ToString();   
                }  
                return oo;   
            } 
    Code-Snippet 1: I was trying to retrieve the antivirus info by using the above code which I wrote.

    I've took a look at this: http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/85df1ceb-48ae-4665-8c8a-bbce3ec11193/

    Have a great day...

    Hope somebody could find the solution.

    Coder24.com
    Sunday, July 27, 2008 7:58 PM

Answers

  •  

    Lingzhi

    Hi,

          I think your codes just need a little modification, and it will work fine.   Please refer to the following codes:

            private string Antivirus(string type)  
            {  
                string computer = Environment.MachineName;  
                string wmipath = @"\\" + computer + @"\root\SecurityCenter";  
                try  
                {  
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath,  
                        "SELECT * FROM AntivirusProduct");  
                    ManagementObjectCollection instances = searcher.Get();  
                    //MessageBox.Show(instances.Count.ToString());  
                    foreach (ManagementObject queryObj in instances)  
                    {  
                        return queryObj[type].ToString();  
                    }  
                }  
     
                catch (Exception e)  
                {  
                    MessageBox.Show(e.Message);  
                }  
     
                return null;  
            }   


          While, before get the AntiVirusProduct information, we should make sure that there is an object of the AntiVirusProduct in our computer.  If there is not an object, I think we should return null here.

          To check whether the object exists, we could use WMI Administrative Tools for help.

          WMI CIM Studio to view WMI Classes

          WMI Object Browser to view WMI Classes objects

          Any more questions, please be free to let me know.  J

     

          Thanks,

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 29, 2008 10:31 AM
  • My

    code solution will soon be found here http://www.coder24.com

    Have a great day....

    Coder24.com
    • Marked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Unmarked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Unmarked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Marked as answer by Fisnik Hasani Tuesday, August 5, 2008 9:07 AM
    Monday, August 4, 2008 1:33 PM

All replies

  •  

    Lingzhi

    Hi,

          I think your codes just need a little modification, and it will work fine.   Please refer to the following codes:

            private string Antivirus(string type)  
            {  
                string computer = Environment.MachineName;  
                string wmipath = @"\\" + computer + @"\root\SecurityCenter";  
                try  
                {  
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath,  
                        "SELECT * FROM AntivirusProduct");  
                    ManagementObjectCollection instances = searcher.Get();  
                    //MessageBox.Show(instances.Count.ToString());  
                    foreach (ManagementObject queryObj in instances)  
                    {  
                        return queryObj[type].ToString();  
                    }  
                }  
     
                catch (Exception e)  
                {  
                    MessageBox.Show(e.Message);  
                }  
     
                return null;  
            }   


          While, before get the AntiVirusProduct information, we should make sure that there is an object of the AntiVirusProduct in our computer.  If there is not an object, I think we should return null here.

          To check whether the object exists, we could use WMI Administrative Tools for help.

          WMI CIM Studio to view WMI Classes

          WMI Object Browser to view WMI Classes objects

          Any more questions, please be free to let me know.  J

     

          Thanks,

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 29, 2008 10:31 AM
  • Hi!

    I'm sorry I haven't been here at MSDN forums for a while now...

    I can't test you solution because I have tested my own which I've created as a class and it worked fine for me...

    Thank you for the replay but I'm sorry I didn't see your solution I'm very bussy working with one big project...

    Thank you but I'd didn't see the solution from you...

     

    Coder24.com
    Monday, August 4, 2008 1:32 PM
  • My

    code solution will soon be found here http://www.coder24.com

    Have a great day....

    Coder24.com
    • Marked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Unmarked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Unmarked as answer by Fisnik Hasani Monday, August 4, 2008 1:33 PM
    • Marked as answer by Fisnik Hasani Tuesday, August 5, 2008 9:07 AM
    Monday, August 4, 2008 1:33 PM
  • Hi, Code24,

           Never mind!  J

           Thank you for your feedback.   I would be glad to go to the website: http://www.coder24.com to see your solution, I think it will be always a great experience to learn from other community member in MSDN forums.  

           Hoping to see you here more.   Thank you again.

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 5, 2008 12:52 AM
  • Hi!

    I'm very sorry It's actually my fault because like I said I haven't been here for a while now.

    And I'm sorry that I didn't see your solution first, I mean your Code-Snipped example I think your version would work to.

    I will soon take care of all MSDN forums questions at http://www.coder24.com.

    Have a great day....

    Regards,
    Fisnik
    Coder24.com
    Tuesday, August 5, 2008 9:04 AM
  •  

    Hi, Code24,

          Thanks, and please permit me to make my solution as answer, because I think it work fine too, like you said.  J

     

         Have a great day too, my friend.

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 5, 2008 9:29 AM
  • Hi, Code24,

          Thanks, and please permit me to make my solution as answer, because I think it work fine too, like you said.  J     
          Have a great day too, my friend.

    Hi!

    Give me your e-mail. We could e-mail and talk about C/C++, C#, VB or VS2005/2008.'

    Have a great day...

    Regards,
    Fisnik




    Coder24.com
    Wednesday, August 6, 2008 9:27 AM
  • Hi,

         You could contact me here: michaelslz AT msn DOT com.  ( Sorry for this, to avoid some junk mails)

         I had a busy working day today, for handling lots of threads here. J

         Any problems, be free to contact me.

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, August 6, 2008 9:57 AM
  • Hi,

    WOW, it's been a while since I saw this post. Now I remember, when I got in touch with Michael Sun (Lingzi Sun), year was 2008. J

    And today, we work on the project "CodeFx" with many others...wow...how things suddenly changes..J

    Have a nice day...J

    Best regards,
    Fisnik
    Coder24.com
    Saturday, May 23, 2009 12:33 PM