确定互联网连接是否可用

时间:2011-10-30 21:34:31

标签: xml visual-studio windows-phone-7 isolatedstorage

)我将xml文件从互联网下载到内存电话..我想查看是否可以通过互联网连接进行下载,如果没有则发送消息。如果不是,我想看看内存中是否已存在xml文件..如果存在,则应用程序不会进行下载。

我有以下代码:

   public MainPage()
    {
        InitializeComponent();
        WebClient downloader = new WebClient();
        Uri xmlUri = new Uri("http://dl.dropbox.com/file_xml.xml", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded);
        downloader.DownloadStringAsync(xmlUri);




    }

    void Downloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the xml-file");
        }
        else
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();


                using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("xml_file.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
                {
                    string xml_file = e.Result.ToString(); 
                    writeFile.WriteLine(xml_file);
                    writeFile.Close();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

NetworkInterface.GetIsNetworkAvailable()方法可以告诉你:

using System.Net.NetworkInformation;
...
if (NetworkInterface.GetIsNetworkAvailable())
{
    // do network-bound stuff
}
else
{
    // notify the user that there is no network connection
}