gpt4 book ai didi

c# - 奇怪的 System.Net.NetworkInformation.NetworkInformationException

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:46 24 4
gpt4 key购买 nike

以下表达式偶尔会抛出以下异常:

NetworkInterface[] interfacesToUse = (from outer in NetworkInterface.GetAllNetworkInterfaces()
select outer).ToArray();

IPv4InterfaceStatistics[] stats = (from current in interfacesToUse select current.GetIPv4Statistics()).ToArray();

Base Exception Type: System.Net.NetworkInformation.NetworkInformationException (0x80004005): The system cannot find the file specified at System.Net.NetworkInformation.SystemIPv4InterfaceStatistics.GetIfEntry(Int64 index) at System.Net.NetworkInformation.SystemNetworkInterface.GetIPv4Statistics() at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at
System.Linq.Buffer
1..ctor(IEnumerable1 source) at
System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source)

Stack Trace: at System.Net.NetworkInformation.SystemIPv4InterfaceStatistics.GetIfEntry(Int64 index) at System.Net.NetworkInformation.SystemNetworkInterface.GetIPv4Statistics() at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at
System.Linq.Buffer
1..ctor(IEnumerable1 source) at
System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source)

我无法找到任何可能提供对此错误的一些见解的文档。

最佳答案

occasionally

我想知道为什么它偶尔会发生。您是否使用如图所示的代码?来自 .NET 3.5 的方法 SystemIPv4InterfaceStatistics.GetIfEntry(Int64 index) 调用 GetIfEntry Iphlpapi.dll 中的函数。从 .NET 4 开始,函数 GetIfEntry2叫做。根据您的堆栈跟踪,我假设您使用的是 .NET 3.5。

错误 2,翻译为“系统找不到指定的文件”(但它实际上只是 ERROR_NOT_FOUND)在将未知索引传递到 GetIfEntry()< 时返回.

这不应该发生在 .NET 中,因为 NetworkInterface.GetAllNetworkInterfaces() 应该只返回系统已知的网络接口(interface),所以它们的所有(私有(private))index 属性应该设置为系统已知的索引。


编辑:重现错误,使用此代码:

var interfaces = NetworkInterface.GetAllNetworkInterfaces();

while (true)
{

foreach (var i in interfaces)
{
var s = i.GetIPv4Statistics();

Console.WriteLine("Received: {0}, Sent: {1}", s.BytesReceived, s.BytesSent);
}
}

当我启动 VPN 连接时,我有一个额外的界面将被打印出来。我第二次禁用此连接时,该接口(interface)上的 GetIPv4Statistics() 将抛出您提到的异常。

我想这取决于您在运行这段代码的机器上做什么。我认为您每次想要获取接口(interface)数据时都必须调用 NetworkInterface.GetAllNetworkInterfaces()

关于c# - 奇怪的 System.Net.NetworkInformation.NetworkInformationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12987589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com