Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether the PC is connected on LAN or not

I want to ask 2 questions and I would be thankful if somebody can reply.

  1. How can I check (using C#) whether the PC is connected to LAN or not?

  2. How can I check (using C#) my PC is connected on LAN or not

like image 572
Asif Avatar asked Mar 13 '10 05:03

Asif


2 Answers

Try

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
like image 184
Bivoauc Avatar answered Sep 19 '22 13:09

Bivoauc


You want to use Ping to check whether a PC is connected to the LAN. Here's a sample:

var ping = new Ping();
var options = new PingOptions { DontFragment = true };

//just need some data. this sends 10 bytes.
var buffer = Encoding.ASCII.GetBytes( new string( 'z', 10 ) );
var host = "127.0.0.1";

try
{
    var reply = ping.Send( host, 60, buffer, options );
    if ( reply == null )
    {
        MessageBox.Show( "Reply was null" );
        return;
    }

    if ( reply.Status == IPStatus.Success )
    {
        MessageBox.Show( "Ping was successful." );
    }
    else
    {
        MessageBox.Show( "Ping failed." );
    }
}
catch ( Exception ex )
{
    MessageBox.Show( ex.Message );
}

To check if you own machine was connected, you could do the same to an address you know should resolve like say the domain controller.

like image 44
Thomas Avatar answered Sep 17 '22 13:09

Thomas