获取本地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 ...
随机推荐
- Python练习笔记(2)
文件读写,多线程.多进程 import time,os,threading,random def file_read(path): try: with open(path, 'r') as f: # ...
- 【GUI】一、Swing外观框架BeautyEye使用
一.Swing外观框架BeautyEye使用 1.1 导包 BeautyEye.jar 1.2 使用BeautyEye L&F public static void main(String[] ...
- Python3 列表,元组,字典,字符串知识小结
一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...
- Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph
题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...
- abap<itab>refresh,clear,free
在ABAP开发过程中,clear,refresh,free都有用来清空内表的作用,但用法还是有区别的. clear itab,清空内表行以及工作区,但保存内存区. clear itab[],清空内表行 ...
- shell重温---基础篇(流程控制&if判断&for&while&循环操作)
和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) { search( ...
- mysql学习第四天(高级查询)
-- 第七章-- 1.查询入职日期最早和最晚的日期select min(hiredate),max(hiredate)from emp -- 2.查询职位以SALES开头的所有员工平均工资,最低工资, ...
- Hibernate-ORM:11.Hibernate中的关联查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的关联查询,及其级联(cascade)操作,以及指定哪一方维护关联关系的(i ...
- [网站公告]18:07-18:20阿里云SLB故障造成网站不能正常访问
(注:由于阿里云SLB管理控制台监控数据不准,实际故障时间是18:07-18:20.) 17:55-18:2018:07-18:20,我们使用的阿里云SLB(负载均衡)中有3台出现突发故障,造成全站无 ...
- 「日常训练」「小专题·图论」Domino Effect(1-5)
题意 分析 这题几乎就是一条dijkstra的问题.但是,如何考虑倒在中间? 要意识到这题求什么:单源最短路的最大值.那么有没有更大的?倒在中间有可能会使它更大. 但是要注意一个问题:不要把不存在的边 ...