如何在C#中获取远程计算机的IP地址 [英] How to get the IP address of Remote Computer in C#

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

问题描述

大家好,



假设有两个系统System1&系统2。 System1 - Windows XP和System2 - Linux。我在System1中安装了WPF应用程序。使用System2的用户将通过与System1的远程连接启动WPF应用程序。



通过WPF应用程序,我可以读取System1的IP地址和Windows登录名通过以下代码。



  private  字符串 GetIP()
{
字符串 strHostName = ;
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress [] addr = ipEntry.AddressList;
return addr [ 0 ]。ToString();
}

String WinUserName_withNetwork = System.Security.Principal.WindowsIdentity.GetCurrent()。Name;
字符串 WinUserNameOnly = System.Environment.UserName;







请帮我看看如何读取System2的IP地址和Windows登录名。

解决方案

试试这个:



  string  goog =   www.google.com; 
IPAddress [] addresslist = Dns.GetHostAddresses(goog);

foreach (IPAddress theaddress in addresslist)
{
Console.WriteLine(theaddress.ToString());
}


你可以试试这个

 strIPAddress = Request.ServerVariables(< span class =code-string>  LOCAL_ADDR


尝试 Request.ServerVariables [REMOTE_ADDR] Request.UserHostAddress



请求对象应该可以在 ASPX 页面的任何位置使用。

Hi All,

Assume there are two systems System1 & System2. System1 - Windows XP and System2 - Linux. I have installed a WPF application in System1. User who is using the System2 will launch the WPF application thru Remote Connection with System1.

Thru WPF application, I can read the IP address and Windows Login Name for the System1 thru the following codes.

private String GetIP()
        {
            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;
            return addr[0].ToString();
        }

String WinUserName_withNetwork = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
String WinUserNameOnly = System.Environment.UserName;




Please help me how to read the IP address and Windows login name for System2.

解决方案

Try this:

string goog = "www.google.com";
IPAddress[] addresslist = Dns.GetHostAddresses(goog);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}


you can try this

strIPAddress = Request.ServerVariables("LOCAL_ADDR")


Try with Request.ServerVariables["REMOTE_ADDR"] or Request.UserHostAddress.

The Request object should be available anywhere in your ASPX page.


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

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