jinyuttt

导航

获取本机IP和mac地址

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
     this->textBox1->Text=Net::Dns::GetHostName();//计算机名称
     String ^str;
     str=Net::Dns::GetHostName();
    

     array<IPAddress^>^ ips;
     ips = Dns::GetHostAddresses(str);
     textBox2->Text=ips[0]->ToString();//获取ip
     IPHostEntry^ host = Dns::GetHostEntry(str);
     this->textBox3->Text=host->AddressList[0]->ToString();
     readmac(textBox2->Text->ToString());
     String   ^strHostIP="";
     IPHostEntry   ^oIPHost=Dns::Resolve(Environment::MachineName);  //获取IP

     strHostIP=oIPHost->AddressList[0]->ToString();
     this->textBox5->Text=strHostIP;

     fetwwip();

    }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    getmach();
   }
   public:

    void   readmac(String ^  ip)//获取mac
    {

     String ^mac = "";
     System::Diagnostics::Process ^p=gcnew System::Diagnostics::Process();

     p->StartInfo->FileName = "nbtstat";
     p->StartInfo->Arguments = "-a " + ip;
     p->StartInfo->UseShellExecute = false;
     p->StartInfo->CreateNoWindow = true;
     p->StartInfo->RedirectStandardOutput = true;            
     p->Start();

     String ^output =p->StandardOutput->ReadToEnd();
     int len = output->IndexOf("MAC Address = ");
     if(len>0)
     {
      mac = output->Substring(len + 14, 17);
     }            
     p->WaitForExit(); 
     this->textBox4->Text=mac;
   
      }

    void fetwwip(void)//获取外网IP
    { 
     System::Net::WebClient  ^wc=gcnew WebClient();
     textBox6->Text =wc->DownloadString("http://www.zu14.cn/ip")->ToString();
        }

 void getmach(void)//获取mac,这种方法需要添加management引用
    {   String    ^stringMAC;
                  String   ^stringIP;
                   
     ManagementClass ^moc=gcnew ManagementClass("Win32_NetworkAdapterConfiguration");
       ManagementObjectCollection  ^mc=moc->GetInstances();
   
    for each (ManagementObject ^mcs in mc  )
    {
     if (Convert::ToBoolean(mcs["ipEnabled"]) == true)
     { 
      
         stringMAC += mcs["MACAddress"]->ToString();
      textBox12->Text=stringMAC->ToString();
         
   
    }
     //throw(gcnew System::NotImplementedException);
  }
 }

由于这里不可以添加源代码。所以源代码传到了csdn。大家也可以加我交流学习啊。

posted on 2010-07-08 08:31  代码苦行僧  阅读(712)  评论(0编辑  收藏  举报