Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 谁将网关IP信息输入文本框_C#_Wpf_Visual Studio_Ip - Fatal编程技术网

C# 谁将网关IP信息输入文本框

C# 谁将网关IP信息输入文本框,c#,wpf,visual-studio,ip,C#,Wpf,Visual Studio,Ip,完全可以在这里。只是学习:) 我做了一些研究,但没有得到答案 我试图在文本框中显示我的网关IP。以下是我的代码(由代码片段构建): 文本框仅显示“:” 现在,如果我使用(从另一个线程复制): 消息框显示IP地址。为什么输出不同?当您在foreach循环中将值分配给TextBox时,最后一项现在可能有IPatall,因此(空IP)将添加到TextBox 因此,请在将项目添加到TextBox 替换此项: Gateway_Address.Text = d.Address.ToString(); 包括

完全可以在这里。只是学习:)

我做了一些研究,但没有得到答案

我试图在文本框中显示我的网关IP。以下是我的代码(由代码片段构建):

文本框仅显示“:”

现在,如果我使用(从另一个线程复制):


消息框显示IP地址。为什么输出不同?

当您在foreach循环中将值分配给
TextBox
时,最后一项现在可能有
IP
atall,因此(空IP)将添加到
TextBox

因此,请在将项目添加到
TextBox

替换此项:

Gateway_Address.Text = d.Address.ToString();
包括:

if(d.Address.ToString().Trim().Length>2)//ignore ::
Gateway_Address.Text = d.Address.ToString();
在第二个代码段中,您使用
MessageBox
显示每个
IP
,因此您可以看到中间的
IP地址

Gateway_Address.Text += d.Address.ToString() + "\r\n";


根据您在这里显示的内容,问题显然不在控件结构中,而是在表单的其他地方。您看到了多少个消息框?您通常会有多个适配器。请解释此答案以使其更可用。有时,我使用string.join+linq快速查看属性值​​对象[]的名称。
if(d.Address.ToString().Trim().Length>2)//ignore ::
Gateway_Address.Text = d.Address.ToString();
Gateway_Address.Text += d.Address.ToString() + "\r\n";
var nis = System.Net.NetworkInformation
            .NetworkInterface.GetAllNetworkInterfaces()
            .Select(s =>
                string.Format("{0}: {1}", s.Name,
                string.Join(";", s.GetIPProperties().GatewayAddresses.Select(ss => ss.Address.ToString()))));

Gateway_Address.Text = string.Join("\r\n", nis);