获取本地ip地址 C#
与ipconfig获取的所有信息一致的方法:
private void GetIp()
{
System.Diagnostics.Process cmdp= new System.Diagnostics.Process();
cmdp.StartInfo.FileName = "ipconfig.exe";//设置程序名
cmdp.StartInfo.Arguments = "/all"; //参数
//重定向标准输出
cmdp.StartInfo.RedirectStandardOutput = true;
cmdp.StartInfo.RedirectStandardInput = true;
cmdp.StartInfo.UseShellExecute = false;
cmdp.StartInfo.CreateNoWindow = true;//不显示窗口---控制台程序是黑屏
//cmdp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思
/*
收集一下,有备无患
关于:ProcessWindowStyle.Hidden 隐藏后如何再显示?
hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow 找到窗口后再ShowWindow
*/
cmd.Start();
string info = cmdp.StandardOutput.ReadToEnd();
cmdp.WaitForExit();
cmdp.Close(); }
单独获取本地ip地址出来的方法:
/// <summary>
/// 获取当前使用的ip
/// </summary>
/// <returns></returns>
public static string GetLocalIp()
{
string result = RunApp("route", "print", true);
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[].Value;
}
else
{
try
{
System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient();
t.Connect("www.baidu.com", );
string ip = ((System.Net.IPEndPoint)t.Client.LocalEndPoint).Address.ToString();
t.Close();
return ip;
}
catch (Exception)
{
return null;
}
}
} /// <summary>
/// 获取本机主DNS
/// </summary>
/// <returns></returns>
public static string GetPrimaryDNS()
{
string result = RunApp("nslookup", "", true);
System.Text.RegularExpressions.Match mat = System.Text.RegularExpressions.Regex.Match(result, @"\d+\.\d+\.\d+\.\d+");
if (mat.Success)
{
return mat.Value;
}
else
{
return null;
}
} /// <summary>
/// 运行一个控制台程序并返回其输出参数。
/// </summary>
/// <param name="filename">程序名</param>
/// <param name="arguments">输入参数</param>
/// <returns></returns>
public static string RunApp(string filename, string arguments, bool recordLog)
{
try
{
if (recordLog)
{
System.Diagnostics.Trace.WriteLine(filename + " " + arguments);
}
System.Diagnostics.Process procezz = new System.Diagnostics.Process();
procezz.StartInfo.FileName = filename;
procezz.StartInfo.CreateNoWindow = true;
procezz.StartInfo.Arguments = arguments;
procezz.StartInfo.RedirectStandardOutput = true;
procezz.StartInfo.UseShellExecute = false;
procezz.Start(); using (System.IO.StreamReader sr = new System.IO.StreamReader(procezz.StandardOutput.BaseStream, Encoding.Default))
{
//string txt = sr.ReadToEnd();
//sr.Close();
//if (recordLog)
//{
// Trace.WriteLine(txt);
//}
//if (!proc.HasExited)
//{
// proc.Kill();
//}
//上面标记的是原文,下面是我自己调试错误后自行修改的
System.Threading.Thread.Sleep(); //貌似调用系统的nslookup还未返回数据或者数据未编码完成 程序就已经跳过直接执行
//txt = sr.ReadToEnd()了,导致返回的数据为空 故睡眠令硬件反应
if (!procezz.HasExited) //在无参数调用nslookup后 可以继续输入命令继续操作 如果进程未停止就直接执行
{ //txt = sr.ReadToEnd()程序就在等待输入 而且又无法输入 直接掐住无法继续运行
procezz.Kill();
}
string txt = sr.ReadToEnd();
sr.Close();
if (recordLog)
System.Diagnostics.Trace.WriteLine(txt);
return txt;
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex);
return ex.Message;
}
}
获取本地ip地址 C#的更多相关文章
- 获取本地IP地址信息
2012-06-05 /// <summary> /// 获取本地IP地址信息 /// </summary> void G ...
- C# — 动态获取本地IP地址及可用端口
1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:
- .net获取本地ip地址
整理代码,.net获取本地ip地址,代码如下: string name = Dns.GetHostName(); IPHostEntry IpEntry = Dns.GetHostEntry(name ...
- 获取本地IP地址的vc代码
作者:朱金灿 来源:http://blog.csdn.net/clever101 获取本地IP地址有两种做法.一种是使用gethostname函数,代码如下: bool CSocketComm::Ge ...
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
获取本地 ip 地址,mac,通过域名获取对应的 ip, 是网络编程可能遇到的比较常见的操作了,所以总结如下(封装了3个函数), 直接上代码: #include <stdio.h> #in ...
- Linux下编程获取本地IP地址的常见方法
转载于:http://blog.csdn.net/k346k346/article/details/48231933 在进行linux网络编程时,经常用到本机IP地址.本文罗列一下常见方法,以备不时之 ...
- .net core获取本地Ip地址的方法
笔记: /// <summary> /// 获取本地Ip地址 /// </summary> /// <returns></returns> public ...
- Java获取本地IP地址
import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...
- python获取本地ip地址的方法
#_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...
随机推荐
- redis 带入的挖矿病毒 qW3xT.2 wnTKYg 解决方法
最近我的阿里云ecs 老是收到 云盾态势感知系统检测到异常 top -c 后发现一个 疑似病毒 /tmp/qW3xT.2 看到网友们的解决方案 试过之后效果不错,可以用的 知道wnTKYg是什么鬼之 ...
- Lavavel5.5源代码 - Pipeline
<?php class Pipeline { protected $passable; protected $pipes = []; protected $method = 'handle'; ...
- 如何在hadoop中使用外部的python程序文件
业务场景大概是这样,我需要在公司hadoop集群上对博文进行结巴分词.我的数据是存储在hive表格中的,数据量涉及到五百万用户三个月内发的所有博文. 首先对于数据来说,很简单,在hive表格中就是两列 ...
- MVC4+EF 列表数据不能绑定
最新准备使用.net 的mvc+Ef来写个项目,开始一切顺利,到了数据绑定时出现了问题. 我的mvc视图引擎是Razor,后台提取数据的是Linq来处理,发现不管怎么样都不能绑定列表数据,可以将后台的 ...
- RedHat6.4 安装yum源
主要参考: http://blog.itpub.net/25313300/viewspace-708509/ http://blog.sina.com.cn/s/blog_50f908410101ct ...
- 一步一步构建手机WebApp开发——页面布局篇
继上一篇:一步一步构建手机WebApp开发——环境搭建篇过后,我相信很多朋友都想看看实战案例,这一次的教程是页面布局篇,先上图: 如上图所示,此篇教程便是教初学者如何快速布局这样的页面.废话少说,直接 ...
- NodeJS微信公众平台开发
微信是手机用户必备的App,微信最开始只是作为社交通讯应用供用户使用,但随着用户量不断的增加,微信的公众号在微信上表现出来了它强大的一面,微信公众平台具有四大优势:1.平台更加稳固:2.用户关系更加平 ...
- jdk带的一些工具,强悍
这些工具有的已经接触到了,功能很强悍,但是使用也有点复杂(参数) 在代码中使用System.setProperty()或者在启动程序时使用-D选项设置代理服务器地址和端口 看看别人的研究: JDK自带 ...
- Qt编译出错:During startup program exited with code 0xc0000135
原文连接:http://blog.csdn.net/wswxfwps/article/details/37317905 出现这个问题,是因为我用到了外部的dll库,lib库我是添加到了.pro文件中了 ...
- Python 3基础教程31-urllib模块
本文介绍Python里的urllib模块,这个urllib主要处理web服务的,如果需要做接口测试,或者写Python的网络爬虫,这个urllib就是最底层的库.需要用到里面的请求方法等. 1. 先看 ...