如何在C#中获取本地IP地址 [英] How to get Local IP Address in C#

查看:790
本文介绍了如何在C#中获取本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取系统的本地IP地址,
其实我用过

How to get the local Ip address of system,
actually I have used

public static string LocalIPAddress()
       {
           IPHostEntry host;
           string localIP = "";
           host = Dns.GetHostEntry(Dns.GetHostName());
           foreach (IPAddress ip in host.AddressList)
           {
               if (ip.AddressFamily.ToString() == "InterNetwork")
               {
                   localIP = ip.ToString();
               }
           }
           return localIP;
       }



但是当我发布代码时,它总是返回已发布系统的IP.
任何帮助将不胜感激!
问候
pawan Sharma



but when i am publishing the code it always returned the published system''s IP.
any help will be apprecited !
Regards
pawan Sharma

推荐答案

尝试一下:
Try this:
public String getIpAddress()
   {
       string strIpAddress;
       strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
       if (strIpAddress == null)
       {
           strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
       }
       return Convert.ToString(strIpAddress);
   }


这将返回其运行的任何系统的IP地址.您正在寻找哪台计算机的IP?
This will return the IP address of whatever system it runs on. Which computer''s IP are you looking for?


您可以使用以下脚本使用sql获取本地IP地址.

执行以下脚本,它将返回客户端系统的IP地址.

You can get local ip address using sql using following script.

Execute below script , it will return ip address of client system.

DECLARE @IP_Address varchar(255)
SELECT @IP_Address = client_net_address FROM sys.dm_exec_connections
WHERE Session_id = @@SPID

print(@IP_Address)


这篇关于如何在C#中获取本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆