I have a class that uses My.Computer.Network.Ping to get a boolean value on whether a IP address or hostname is reachable.
However, when I’m testing that class, I want to disable pinging so that during the test whenever I run the My.Computer.Network.Ping it will always return false or throw some kind of exception.
I have tried the following, but I still get a true returned (meaning, the attempts to disable ping didn’t work)
Dim restricited As New System.Net.NetworkInformation.NetworkInformationPermission(System.Security.Permissions.PermissionState.None)
Dim mySocketPermission1 As New SocketPermission(PermissionState.None)
Dim myWebPermission As New WebPermission(PermissionState.None)
myWebPermission.Demand()
mySocketPermission1.Demand()
Dim ping As New PingOptions()
ping.DontFragment = True
ping.Ttl = 1
*attempt to ping here*
*but doesn't return the "ping failed" I wanted*
Any ideas?
Edit: ‘m using VS2010 with .Net 4.0 Framework (full)
Paul B’s answer did it for me. I’m testing on Win7 and XP, but XP seems to be fussy when using the host file method. Works fine on Win7.
Paul is the man.