Saturday, March 21, 2015
Quering IP Address
using System.Windows.Forms;
using System.Net; //Added
using System.Net.Sockets; //Added
namespace Quering_IPAddresses
{
public partial class Form1 : Form
{
/// <summary>
/// Using the System.Net namespace
/// Looking at the Dns class.
/// </summary>
public Form1()
{
InitializeComponent();
}
private void btnGetLocalIPAddress_Click(object sender, EventArgs e)
{
///Use Dns class defined in System.Net namespace
///Use the GetHostName method to get the name of this (local) computer
string hostname = Dns.GetHostName();
//Get this (local) host IPAddress
//You can use the method (defined in the Dns class):
///public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
IPAddress[] ipaddresses = Dns.GetHostAddresses(hostname);
//display the local host name
richTextBox1.Text = "local hostname : " + hostname + "\n\n";
//display its ipaddresses
foreach (IPAddress address in ipaddresses)
{
richTextBox1.AppendText(address + "\n");
}
}
private void btnIPAddressesOfHostname_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
try
{
//get hostname from user
string hostname = txtHostName.Text;
//display its ip addresses
IPAddress[] ipaddresses = Dns.GetHostAddresses(hostname);
//display its ipaddresses
foreach (IPAddress address in ipaddresses)
{
richTextBox1.AppendText(address + "\n");
}
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void btnDownLoadPage_Click(object sender, EventArgs e)
{
try
{
///Use the WebClient class defined in the System.Net namespace
///The WebClient defines a method: DownloadString
///Create a WebClient object
WebClient client = new WebClient();
///Use the DownLoadString method and pass it the address
string result = client.DownloadString(txtAddress.Text);
//display
richTextBox2.Text = result;
}
catch (WebException we)
{
//catch this exception in case there was an error while downloading
MessageBox.Show(we.Message);
}
}
}
}
Labels:
C#Advance
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment