C#获取IP
2016-07-11 15:42
381 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestIP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string iphostname = System.Net.Dns.GetHostName();
//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
//System.Net.IPAddress[] ipaddress = System.Net.Dns.GetHostByName(iphostname).AddressList;
// //会返回所有地址,包括IPv4和IPv6
System.Net.IPAddress[] ipaddress = System.Net.Dns.GetHostAddresses(iphostname);
if (ipaddress != null && ipaddress.Length > 0)
{
foreach (System.Net.IPAddress ip in ipaddress)
{
this.richTextBox1.Focus();
SendKeys.Send(ip.ToString());
SendKeys.Send("{ENTER}");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string iphostname = System.Net.Dns.GetHostName();
System.Net.IPHostEntry iphostentry = System.Net.Dns.GetHostEntry(iphostname);
System.Net.IPAddress[] ipaddress = iphostentry.AddressList;
if (ipaddress != null && ipaddress.Length > 0)
{
foreach (System.Net.IPAddress ip in ipaddress)
{
this.richTextBox1.Focus();
SendKeys.Send(ip.ToString());
SendKeys.Send("{ENTER}");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "ipconfig.exe";
process.StartInfo.Arguments = "/all";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
//process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
//hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
//Win32Native.ShowWindow(hwndWin32Host, 1);
//先FindWindow找到窗口后再ShowWindow
process.Start();
string xx = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
this.richTextBox1.Text = xx;
}
private void button4_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string urlLink = "http://ip138.com/";
//www.ip138.com
System.Uri url = new Uri(urlLink);
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
//System.Net.WebRequest request1 = System.Net.HttpWebRequest.Create(url);
System.IO.Stream respon = request.GetResponse().GetResponseStream();
System.IO.StreamReader read = new System.IO.StreamReader(respon, Encoding.GetEncoding("gb2312"));
string xxx = read.ReadToEnd();
int cc = xxx.IndexOf("[");
int cc1 = xxx.IndexOf("]");
this.richTextBox1.Text = xxx;
}
}
}
运行界面如下
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestIP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string iphostname = System.Net.Dns.GetHostName();
//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
//System.Net.IPAddress[] ipaddress = System.Net.Dns.GetHostByName(iphostname).AddressList;
// //会返回所有地址,包括IPv4和IPv6
System.Net.IPAddress[] ipaddress = System.Net.Dns.GetHostAddresses(iphostname);
if (ipaddress != null && ipaddress.Length > 0)
{
foreach (System.Net.IPAddress ip in ipaddress)
{
this.richTextBox1.Focus();
SendKeys.Send(ip.ToString());
SendKeys.Send("{ENTER}");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string iphostname = System.Net.Dns.GetHostName();
System.Net.IPHostEntry iphostentry = System.Net.Dns.GetHostEntry(iphostname);
System.Net.IPAddress[] ipaddress = iphostentry.AddressList;
if (ipaddress != null && ipaddress.Length > 0)
{
foreach (System.Net.IPAddress ip in ipaddress)
{
this.richTextBox1.Focus();
SendKeys.Send(ip.ToString());
SendKeys.Send("{ENTER}");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "ipconfig.exe";
process.StartInfo.Arguments = "/all";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
//process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//关于:ProcessWindowStyle.Hidden隐藏后如何再显示?
//hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
//Win32Native.ShowWindow(hwndWin32Host, 1);
//先FindWindow找到窗口后再ShowWindow
process.Start();
string xx = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
this.richTextBox1.Text = xx;
}
private void button4_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
string urlLink = "http://ip138.com/";
//www.ip138.com
System.Uri url = new Uri(urlLink);
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
//System.Net.WebRequest request1 = System.Net.HttpWebRequest.Create(url);
System.IO.Stream respon = request.GetResponse().GetResponseStream();
System.IO.StreamReader read = new System.IO.StreamReader(respon, Encoding.GetEncoding("gb2312"));
string xxx = read.ReadToEnd();
int cc = xxx.IndexOf("[");
int cc1 = xxx.IndexOf("]");
this.richTextBox1.Text = xxx;
}
}
}
运行界面如下
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法