獲取服務器ip,本地ip,本在mac

 

本文來自:http://www.cnblogs.com/lilei107/archive/2011/07/27/2118506.html

using System.Net;
// 在此處放置用戶代碼以初始化頁面
    string SessionID = HttpContext.Current.Request.UserHostAddress;

    string strHostName = Dns.GetHostName(); //得到本機的主機名
    IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機IP 
    SessionID = SessionID +","+ ipEntry.AddressList[0].ToString();
    Label1.Text = SessionID;

   System.Diagnostics.Process p=new System.Diagnostics.Process();
   p.StartInfo.CreateNoWindow=true;
   p.StartInfo.UseShellExecute=false;
   p.StartInfo.RedirectStandardOutput=true;
   p.StartInfo.FileName="ipconfig";
   p.StartInfo.Arguments="/all";
   p.Start();
   p.WaitForExit();
   string s=p.StandardOutput.ReadToEnd();
   Label1.Text+=","+s.Substring(s.IndexOf("Physical Address. . . . . . . . . :")+36,17);

------------------------
//
            //獲取指定域名的IP地址
            //
            string StringURL;
            StringURL = "http://www.baidu.com/";
            for (int i = 0; i < Dns.GetHostEntry(StringURL).AddressList.Length; i = i + 1)
            {
                MessageBox.Show(Dns.GetHostEntry(StringURL).AddressList[i].ToString());
            }


            //
            //獲取本機IP地址
            //
            string StringHost;
            StringHost = Dns.GetHostName();
            for (int i = 0; i < Dns.GetHostEntry(StringHost).AddressList.Length; i = i + 1)
            {
                MessageBox.Show(Dns.GetHostEntry(StringHost).AddressList[i].ToString());
            }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章