获取本地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起源与发展
Python的创始人为吉多*范罗苏姆(Gudio van Rossum) 1.1989年的圣诞节期间,吉多*范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的解释程序,作为ABC语言的一种继承. 2. ...
- 一个好用的C# HttpHelper类
/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提 ...
- 如何在hadoop中使用外部的python程序文件
业务场景大概是这样,我需要在公司hadoop集群上对博文进行结巴分词.我的数据是存储在hive表格中的,数据量涉及到五百万用户三个月内发的所有博文. 首先对于数据来说,很简单,在hive表格中就是两列 ...
- 002---Linux系统目录结构
Linux系统目录结构 一切从根(/)开始,一切皆文件. /bin:存放常用的可执行文件 /sbin:存放常用的可执行文件 家目录:存放用户自己的文件或目录 root用户:/root 普通用户:/ho ...
- UVA - 12230
#include <bits/stdc++.h> using namespace std; int n; double d; double p,l,v,ret,sum; ; /* 村庄A, ...
- Android ANR 分析
首先贴一下trace 文件 Process: com.oppo.reader PID: 20358 Time: 2933175644_1545041895232 Flags: 0x38d83e44 P ...
- Python的logging模块、os模块、commands模块与sys模块
一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...
- EF报错“EntityValidationErrors”
在使用EF更新实体的时候报错,显示界面如下: 点击查看详情: 在查看详细的窗体中,EntityValidationErrors里面的也看不到具体的错误原因.在网上 ...
- 生产者与消费者-N:1-基于list
多个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- Uniy 组件式泛型单例模式
我们知道,在Unity中,所有对象脚本都必须继承MonoBehavior脚本,才能使用Unity内置的脚本功能; 通常我们可以用静态类来取代单例模式,但是静态类方法的缺点是,它们必须继承最底层的类-- ...