using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;namespace 查自己电脑的_IP_地址
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void btnChaXun_Click(object sender, EventArgs e)
        {
            tbResult.Clear();
            ProcessStartInfo start = new ProcessStartInfo();
            //start.Arguments = "ipconfig";
            start.FileName = "cmd.exe";
            start.CreateNoWindow = true;
            start.UseShellExecute = false;
            start.RedirectStandardInput = true;
            start.RedirectStandardOutput = true;
            Process p = Process.Start(start);
            p.StandardInput.WriteLine("ipconfig");
            StreamReader reader = p.StandardOutput;
            string s = reader.ReadLine();
            while (!reader.EndOfStream)
            {
                tbResult.AppendText(s + "\n");
                s = reader.ReadLine();
            }
            p.WaitForExit();
            p.Close();
            reader.Close();
        }        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

解决方案 »

  1.   

    本帖最后由 caozhy 于 2012-09-09 13:56:46 编辑
      

  2.   

    直接运行ipconfig就行,不用启动cmd,再通过cmd运行ipconfig。
    对于caozhy的方法,win7如何不正确了?
      

  3.   


    那确实需要修改。需要遍历其结果,不可能保证AddressList[0]一定是一个ipv4的地址,更不可能保证就一定是别人需要访问的ipv4地址。例如我的机器上安装了虚拟机,它就自动注册了3个网络适配器。同时我的机器有时候其无线网卡和RJ45接头的网卡都开着。实际上什么叫做“自己电脑的IP”?你能给我一个真正的解释吗?
      

  4.   

    本帖最后由 caozhy 于 2012-09-09 23:13:39 编辑
      

  5.   

    我的就是win7丫。不能用2楼的方法吗