The following expression is occasionally throwing the below exception:
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() at1..ctor(IEnumerable
System.Linq.Buffer1 source) at1 source)
System.Linq.Enumerable.ToArray[TSource](IEnumerableStack Trace: at
System.Net.NetworkInformation.SystemIPv4InterfaceStatistics.GetIfEntry(Int64
index) at
System.Net.NetworkInformation.SystemNetworkInterface.GetIPv4Statistics()
at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at1..ctor(IEnumerable
System.Linq.Buffer1 source) at1 source)
System.Linq.Enumerable.ToArray[TSource](IEnumerable
I have been unable to locate any documentation that may provide some insight into this error.
I wonder why it occasionally happens. Do you use the code as shown? The method
SystemIPv4InterfaceStatistics.GetIfEntry(Int64 index), which is from .NET 3.5, calls theGetIfEntryfunction inIphlpapi.dll. Starting with .NET 4, the functionGetIfEntry2is called. According to your stack trace, I assume you’re using .NET 3.5.Error 2, which is translated to “The system cannot find the file specified” (but it really is just
ERROR_NOT_FOUND) is returned when an unknown index is passed intoGetIfEntry().This should not happen in .NET, since
NetworkInterface.GetAllNetworkInterfaces()should only return network interfaces already known to the system, so all their (private)indexproperties should be set to an index known to the system.Edit: reproduced the error, using this code:
When I start a VPN connection I have an extra interface which will be printed. The second I disable this connection, the
GetIPv4Statistics()on that interface will throw the exception you mention.I guess it depends on what you’re doing on the machine this code runs on. I think you’ll have to call
NetworkInterface.GetAllNetworkInterfaces()every time you want to get the interface data.