收集客户机信息时,需要得到客户机的IP,那么客户端就需要提供自己的本机IP:
using System;
using System.Net;
using System.Windows.Forms;
namespace Mynet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetMyIP());
}
/// <summary>
/// 得到本机IP
/// </summary>
/// <returns></returns>
public string GetMyIP()
{
try
{
string computername = Dns.GetHostName();
IPHostEntry ipall = Dns.GetHostEntry(computername);
for (int i = 0; i < ipall.AddressList.Length; i++)
{
string theip = ipall.AddressList[i].ToString();
if (theip.LastIndexOf(".") != -1)
{
return theip;
}
}
return "未得到IP地址";
}
catch (Exception ErrMsg)
{
return ErrMsg.ToString();
}
}
}
}