如何将ConsoleWrite更改为标签文本

时间:2018-05-05 11:28:20

标签: c#

我一直在使用我的IP Get和console.write功能,但是有一个问题,我不知道Console.Write做了什么,我只需要代码将转到Label文本,这意味着当IP到达时,它将转到Label.Text 顺便说一句,我使用Visual Studio c#。

private void metroLabel1_Click(object sender, EventArgs e)
    {
         String strHostName = string.Empty;
         // Getting Ip address of local machine...
         // First get the host name of local machine.
         strHostName = Dns.GetHostName();
         Console.WriteLine("Local Machine's Host Name: " + strHostName);
         // Then using host name, get the IP address list..
         IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
         IPAddress[] addr = ipEntry.AddressList;

         for (int i = 0; i < addr.Length; i++)
         {
             Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            //metroLabel1.Text = " >> " + "", i, addr[i].ToString());
           // Console.WriteLine = metroLabel1.Text;

        }
         Console.ReadLine();



    }

2 个答案:

答案 0 :(得分:1)

试试这个

metroLabel1.Text = inputString

答案 1 :(得分:0)

试试这个......

string HostName = Dns.GetHostName();   
IPAddress[] ipaddress = Dns.GetHostAddresses(HostName); 

//Label lblIPlist = new Label();  

foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork))  
{  
    //Console.WriteLine(ip4.ToString());  
    metroLabel1.Text += "IP Address: " + ip4.ToString() + "\n";
}  

//Console.WriteLine(metroLabel1.Text);


//foreach (IPAddress ip6 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))  
//{  
//  Console.WriteLine(ip6.ToString());  
//}  

参考this site。它可能对你有用。

<子>参考
- https://www.c-sharpcorner.com/UploadFile/1e050f/getting-ip-address-and-host-name-using-dns-class/

相关问题