我是创建针对平板电脑的应用程序的新手。我有一个项目,平板电脑可能在任何给定点都没有网络连接。我想监控网络可用性状态并相应地在UI上启用/禁用功能。我做了一些阅读并发现了 使用System.Net.NetworkInformation。从我读过的内容来看,它似乎就像我想要使用的那样,所以我根据我读过的文章/帖子添加了它。
我在.NET 4.0上使用Visual Studio 2013运行64位Win7 Enterprise SP1。我正在运行的机器是Intel i3-2120。网络适配器是Intel 82579LM。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;
using System.Windows.Media.Effects;
using System.ComponentModel;
namespace GWL{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.Loaded += delegate { MainWindowLoaded(); };
}
private void MainWindowLoaded()
{
NetworkIsAvailable = NetworkInterface.GetIsNetworkAvailable();
try
{
NetworkChange.NetworkAvailabilityChanged += delegate { NetworkIsAvailable = NetworkInterface.GetIsNetworkAvailable(); };
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
当处理归结为NetworkChange.NetworkAvailabilityChanged侦听器时,我收到以下套接字错误:
System.Net.Sockets.SocketException Error Code 10022: "An invalid argument was supplied" at System.Net.SafeCloseSocketAndEvent.CreateWSASocketWithEvent(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, Boolean autoReset, Boolean signaled) in System.
完成错误消息:
System.Net.Sockets.SocketException occurred
_HResult=-2147467259
_message=An invalid argument was supplied
HResult=-2147467259
IsTransient=false
Message=An invalid argument was supplied
Source=System
ErrorCode=10022
NativeErrorCode=10022
StackTrace:
at System.Net.SafeCloseSocketAndEvent.CreateWSASocketWithEvent(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, Boolean autoReset, Boolean signaled)
InnerException:
在将委托分配给NetworkAvailabilityChanged或者我错过的进一步配置时,是否有一些我缺少的东西?任何帮助表示赞赏。提前谢谢。
更新:此问题似乎与项目位于网络驱动器上的事实有关。我将它移动到本地存储,并且不会发生错误。当移回网络驱动器时,它会再次显示。