C# 获取IPCONFIG 返回值

在我们获取本机局域网IP以及其他相关信息时,直接调用系统IPCONFIG,也是一种很有效的方法。

以下是我用C#实现的 读取ipconfig的返回值的代码:

 

 

ExpandedBlockStart.gif 代码
///   <summary>
        
///  获取IPCONFIG返回值
        
///   </summary>
        
///   <returns> 返回 IPCONFIG输出 </returns>
         public   static   string  GetIPConfigReturns()
        {
            
string  version  =  System.Environment.OSVersion.VersionString;

            
if  (version.Contains( " Windows " ))
            {
                
// 调用ipconfig ,并传入参数: /all
                System.Diagnostics.ProcessStartInfo psi  =   new  System.Diagnostics.ProcessStartInfo( " ipconfig " " /all " );

                psi.CreateNoWindow 
=   true // 若为false,则会出现cmd的黑窗体
                psi.RedirectStandardOutput  =   true ;
                psi.UseShellExecute 
=   false ;

                System.Diagnostics.Process p 
=  System.Diagnostics.Process.Start(psi);

                
return  p.StandardOutput.ReadToEnd();
            }

            
return   string .Empty;
        }

 

以下是返回的结果:

 

ExpandedBlockStart.gif 代码
/* 返回结果

        Windows IP Configuration



           Host Name . . . . . . . . . . . . : server

           Primary Dns Suffix  . . . . . . . : 

           Node Type . . . . . . . . . . . . : Unknown

           IP Routing Enabled. . . . . . . . : No

           WINS Proxy Enabled. . . . . . . . : No



        Ethernet adapter 本地连接:



           Connection-specific DNS Suffix  . : 

           Description . . . . . . . . . . . : NVIDIA nForce 10/100 Mbps Ethernet 

           Physical Address. . . . . . . . . : 00-E0-4C-BB-4F-AE

           DHCP Enabled. . . . . . . . . . . : No

           IP Address. . . . . . . . . . . . : 192.168.1.26

           Subnet Mask . . . . . . . . . . . : 255.255.255.0

           Default Gateway . . . . . . . . . : 192.168.1.1

           DNS Servers . . . . . . . . . . . : 202.103.24.68

                                               202.103.44.150
          
*/

扩展说明:

这里我们调用的是IPCONFIG,其实就是想在运行里面输入IPCONFIG一样的效果。既然这样我们就可以延伸的去调用其他的 应用程序,并可获得调用的应用程序的输出。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值