WebClient を使用して Web サイトからデータを取得したり、ping を使用したり、ネットワーク インターフェイスを確認したりできます。
Web クライアント
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new System.Uri("http://google.com"));
エラーの確認:
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("An Error has occured. Maybe the website you are trying to reach is offline or you have no internetconnection");
});
}
}
ピン
public bool CheckInternetConnection()
{
bool success = false;
using (Ping ping = new Ping())
{
try
{
if (ping.Send("google.com", 2000).Status == IPStatus.Success)
{
success= true;
}
}
catch (PingException)
{
success = false;
}
}
return success;
}
ネットワークインターフェースを確認してください
using Microsoft.Phone.Net.NetworkInformation;
private void CheckInetConnection()
{
if (NetworkInterface.GetIsNetworkAvailable() == true)
{
//Internet avalaible
}
else
{
//No connection available
}
}
次のコードを使用して、C# でアプリを閉じることができます。
Application.Current.Exit();