阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# 得到電腦IP及電腦名

Posted on 2007-09-03 21:07  宽田  阅读(1356)  评论(0编辑  收藏  举报

       using System.Net;

        /// <summary>
        
/// 得到用戶電腦信息
        
/// </summary>
        public void GetUserInfo()
        {
            
string sIP = "";

            
//得到計算機名 方法1
            string MachineName = System.Environment.MachineName;
            
//得到計算機名 方法2
            string strHostName = Dns.GetHostName();

            IPAddress[] addr;

            
//得到IP 方法1
            
//addr = Dns.GetHostAddresses(strHostName);
            
//得到IP 方法2
            addr = Dns.GetHostAddresses("127.0.0.1");
            
//得到IP 方法3
            
//addr = Dns.GetHostAddresses("localhost");

            
//得到IP 方法4
            
//IPHostEntry ihIP = Dns.GetHostEntry(strHostName);
            
//addr = ihIP.AddressList;

            
//得到IP 方法5 此句顯示老式語句警告
            addr = Dns.GetHostByName(Dns.GetHostName()).AddressList;

            
if (addr.Length<=1)
            {
                sIP 
= addr[0].ToString();
            }
            
else
            {
                
for (int iCount = 0; iCount < addr.Length; iCount++)
                {
                    sIP 
+= "IP" + (iCount + 1).ToString() + ":" + addr[iCount].ToString() + " ";
                }
            }           
        }