共用方式為


HOW TO:顯示 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 位址。

請參閱

工作

連接問題疑難排解

其他資源

設定及使用裝置模擬器