如何:显示 Smartphone 仿真程序的 IP 地址

更新:2007 年 11 月

发掘 Smartphone 仿真程序的 IP 地址需要编程方法。下列步骤演示如何创建和运行一个这样的例程。

创建例程

  1. 在 Visual Studio 中,打开一个新的 C# Smartphone 空项目。

  2. 在“解决方案资源管理器”中,右击项目,指向“添加”,然后单击“新建项”。

  3. 单击“代码文件”,然后单击“添加”。

    代码编辑器打开空白页面。

  4. 将以下代码块复制到编辑器页面。

    using System;
    using System.Net;
    using System.Text;
    using System.Windows.Forms;
    
    public class GetAddress
    {
        /// <summary>
        /// A sample application that displays a list of IP addresses 
        /// that are bound to the current device.
        /// </summary>
    
        static void Main()
        {
            try
            {
                IPHostEntry IPHost = Dns.Resolve(Dns.GetHostName());
                IPAddress[] addressList = IPHost.AddressList;
    
                if (addressList.Length > 0)
                {
                    StringBuilder address = new StringBuilder();
                    foreach (IPAddress a in addressList)
                    {
                        address.Append(a.ToString());
                        address.Append(" ");
                    }
                    MessageBox.Show(address.ToString(), "IP Addresses");
                }
    
                else
                    MessageBox.Show("Unable to determine network address", "Error");
            }
    
            catch (Exception)
            {
                MessageBox.Show("Unable to determine network address", "Error");
            }
        }
    }
    
  5. 在“解决方案资源管理器”中,右击“引用”,然后单击“添加引用”。

  6. 单击“System.Windows.Forms”,然后单击“确定”。

运行例程

  1. 在“调试”菜单上单击“启动调试”。

  2. 在“部署”对话框中,单击要显示其 IP 地址的 Smartphone 仿真程序。

  3. 单击“部署”。

    应用程序显示 IP 地址。

请参见

任务

连接问题疑难解答

其他资源

配置和使用设备仿真程序