与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#的更多相关文章

  1. 获取本地IP地址信息

    2012-06-05    /// <summary>         /// 获取本地IP地址信息         /// </summary>         void G ...

  2. C# — 动态获取本地IP地址及可用端口

    1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:

  3. .net获取本地ip地址

    整理代码,.net获取本地ip地址,代码如下: string name = Dns.GetHostName(); IPHostEntry IpEntry = Dns.GetHostEntry(name ...

  4. 获取本地IP地址的vc代码

    作者:朱金灿 来源:http://blog.csdn.net/clever101 获取本地IP地址有两种做法.一种是使用gethostname函数,代码如下: bool CSocketComm::Ge ...

  5. Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip

    获取本地 ip 地址,mac,通过域名获取对应的 ip, 是网络编程可能遇到的比较常见的操作了,所以总结如下(封装了3个函数), 直接上代码: #include <stdio.h> #in ...

  6. Linux下编程获取本地IP地址的常见方法

    转载于:http://blog.csdn.net/k346k346/article/details/48231933 在进行linux网络编程时,经常用到本机IP地址.本文罗列一下常见方法,以备不时之 ...

  7. .net core获取本地Ip地址的方法

    笔记: /// <summary> /// 获取本地Ip地址 /// </summary> /// <returns></returns> public ...

  8. Java获取本地IP地址

    import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...

  9. python获取本地ip地址的方法

    #_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...

随机推荐

  1. php GD图片四角圆形处理

    <?php /** * blog:http://www.zhaokeli.com * 处理四角圆图片 * @param string $imgpath 源图片路径 * @param intege ...

  2. Phpstrom开发工具

    下载地址 https://www.jetbrains.com/zh/phpstorm/specials/phpstorm/phpstorm.html?utm_source=baidu&utm_ ...

  3. hadoop生态搭建(3节点)-03.zookeeper配置

    # https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html # ===== ...

  4. pygame小游戏之坦克大战

    以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5  pycharm import pygame,sys,time from random ...

  5. linux课后作业1

    本实验6第一题:菜单驱动程序. 随便进到某个目录,vim driver.sh 把代码写进去. #!/bin/bash function welcome() { echo -e "\n&quo ...

  6. Javaweb——四则运算---18.11.01

    ---恢复内容开始--- test.jsp <%@ page language="java" contentType="text/html; charset=utf ...

  7. 6 wireshark 安装使用 数据包抓取

    1.wireshark安装 2.开始使用 3.界面详情 4. 数据包抓取 5.过滤数据

  8. CC3200作为STA模式连接路由器sl_WlanConnect出现exception occured at:0xa72fcf6

    1. 先看下出错的提示 2. 出错的代码部分,现在问题是定位不到哪一行代码出问题,反正运行一段时间就进入了 lRetVal = sl_WlanConnect((signed , &secPar ...

  9. CentOS修改网卡名称

    转 一.问题说明 测试环境中出现的小问题,因为虚拟机之间经常复制来复制去,导致网卡配置这块的不一致现象. 配置文件的信息: [root@ora10g network-scripts]# catifcf ...

  10. How to add a webpart to your website

          I have download a webpart that can play media on the website from the internet.Then how to add ...