获取本地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 ...
随机推荐
- thinkphp5阿里大于短信接口
function autumn_sendsms($tel,$stype){ $pd_go=true; if($tel==''){ $msg='手机号不能为空'; $pd_go=false; } if( ...
- 【Js】Jquery遍历-each(function(e){})中的e和$(this)的区别
$("selector").each(function(e){ console.log($(e)); console.log($(this)); console.log(e); c ...
- hadoop搭建----centos免密码登录、修改hosts文件
分布式系统在传输数据时需要多台电脑免密码登录 如:A(192.168.227.12)想ssh免密码登录到B(192.168.227.12),需要把A的公钥文件(~/.ssh/id_rsa.pub)里内 ...
- IO复用——epoll系列系统调用
1.内核事件表 epoll是Linux特有的I/O复用函数.epoll把用户关心的文件描述上的事件放在内核里的一个事件表中,并用一个额外的文件描述符来标识该内核事件表.这个额外文件描述符使用函数epo ...
- 在amazon linux上安装Jenkins
原文请参考: https://medium.com/@itsmattburgess/installing-jenkins-on-amazon-linux-16aaa02c369c
- linux进程 生产者消费者
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> # ...
- kafka topic 完全删除
kafka topic 完全删除 1.自动删除脚本(得配置server.properties 中 delete.topic.enable=true) ./kafka-topics.sh --zoo ...
- EIP权限工作流平台总结-1总体说明
预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...
- SQL Server 2005 导出包含(insert into)数据的SQL脚本 (使用存储过程) 分类: 数据库
CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS ) ) ) declare @xtype tinyint declare @name ...
- 3招搞定APP注册作弊
在说如何应对之前,易盾先给各位盾友梳理移动端APP可能遇到哪些作弊风险.1. 渠道商刷量,伪造大量的下载量和装机量,但没有新用户注册:2. 对于电商.P2P.外卖等平台,会面临散户或者团队刷子的注册- ...