如何获取远程计算机的IP地址

时间:2013-06-04 10:59:31

标签: c#

假设有两个系统; System1和System2。

  • System1 - Windows XP
  • System2 - Linux。

我在System1上安装了WPF应用程序。 System2上的用户通过远程桌面连接连接到System1并启动WPF应用程序。

在WPF应用程序中,我可以使用以下代码获取System1的本地IP地址和Windows登录名。

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;

我的问题是我想要从System2登录的用户的IP地址和用户名。

要获取该IP地址和用户名,我需要做什么?

3 个答案:

答案 0 :(得分:1)

据我所知,您想知道System1计算机中System2的IP地址以及通过远程连接记录的用户名。我是对的吗?

假设您可以使用环境变量来收集此信息:

  1. CLIENTNAME:包含通过远程桌面连接的计算机的名称。
  2. USERNAME:包含登录用户的名称。
  3. 希望这有帮助。

答案 1 :(得分:0)

请参阅以下代码:

IPHostEntry iPAddress = DNS.GetHostByName (HostName);
 IPAddress [] IPAdd = iPAddress.AddressList;

 for (int j = 0; j < IPAdd.Length; j++)
 {
  Console.WriteLine ("IP Address {0}: {1} ", j, IPAdd[j].ToString ());
 }

也可以参考THIS doccument。

答案 2 :(得分:0)

只需打开命令提示符/ shell并键入IPCONFIG

即可

寻找IP4地址。很多时候你会看到它是“127.0.0.1”。键入NSLOOKUP以获取计算机的名称。

希望这有帮助

相关问题