C#获取ip的示例
2016-07-09 18:12
387 查看
界面
![](https://images2015.cnblogs.com/blog/694927/201607/694927-20160711151337920-1052719573.png)
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;
}
}
}
![](https://images2015.cnblogs.com/blog/694927/201607/694927-20160711151337920-1052719573.png)
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;
}
}
}
相关文章推荐
- 用C#语言实现八皇后问题
- 将XNA整合到WinForm
- C#经验
- C#中取得Web的当前目录
- C#将文字写入图片
- C#基础之Attribute
- C#获取本机IP以及无线网ip
- C#字符串反序
- C#的语法
- C#中的反射原理及应用
- c#新手入门级代码分享 在类进行实例化的时候初始化字段
- C#的抽象类和接口的区别,在什么时候使用才合适?
- XNA載入圖片策略與效能
- c#新手入门级代码分享 只读,只写,读写,读写时过滤的属性
- php示例代码之类似于C#中的String.Format方法
- C#类型基础
- 《C#并发编程经典实例》笔记
- 【知识积累】C#值类型和引用类型区别
- c#三个数是否能构成三角形
- 表格汇总器(C#)