Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys, I need to get the status of windows defender, but I don't know how to do this with vb.net, can you help me with this?
I'm using this code, but it's not quite how I want it.

What I have tried:

Dim av_searcher As New ManagementObjectSearcher("root\SecurityCenter2", "SELECT * FROM AntivirusProduct")

       For Each info As ManagementObject In av_searcher.Get()
           MsgBox(info.Properties("displayName").Value.ToString())

           Dim AvStatus = Hex(info.Properties("ProductState").Value.ToString())
           If Mid(AvStatus, 2, 2) = "10" Or Mid(AvStatus, 2, 2) = "11" Then
               MsgBox("AntiVirus enabled")
           ElseIf Mid(AvStatus, 2, 2) = "00" Or Mid(AvStatus, 2, 2) = "01" Then
               MsgBox("AntiVirus disabled")
           End If

       Next info
Posted
Updated 12-Mar-20 10:58am

1 solution

Your problem is here:
VB.NET
Dim AvStatus = Hex(info.Properties("ProductState").Value.ToString())

You're converting the ProductState value to a string, which will be a decimal number string, not a decimal NUMBER, which is what the Hex function wants.

You're nesting method calls together, making it more difficult to debug the code. Break the line out into individual statements so you can see what you're really dealing with in the debugger.
VB.NET
Dim productStateValue As Integer = DirectCast(info.Properties("ProductState").Value, Integer)
Dim productState As String = Hex(productStateValue)

My VB is quite rusty, so this may not work as written, but you get the idea.
 
Share this answer
 
v2
Comments
Maciej Los 13-Mar-20 7:28am    
5ed!
RafaelFranckilin 13-Mar-20 7:41am    
I would like to get only the status of windows defender, my current code takes the status of any installed antivirus.
phil.o 13-Mar-20 7:51am    
You could use a WHERE clause to further filter the results:
Dim av_searcher As New ManagementObjectSearcher("root\SecurityCenter2", "SELECT * FROM AntivirusProduct WHERE displayName=""Windows Defender""")
RafaelFranckilin 13-Mar-20 8:33am    
It solved my problem, thank you very much.
phil.o 13-Mar-20 9:48am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900