确定互联网连接是否可用

发布于 2024-12-12 21:54:31 字数 1224 浏览 0 评论 0原文

)我将 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();
                }
            }
        }
    }

) I make the download of an xml file from the internet to the memory phone.. I want to see if the internet connection is available to make the download and send a message if not. And if not i want to see if the xml file as already present in the memory.. if present, the appliccation doesn't make the download.

I have the following code:

   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();
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-12-19 21:54:32

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

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

The NetworkInterface.GetIsNetworkAvailable() method can tell you this:

using System.Net.NetworkInformation;
...
if (NetworkInterface.GetIsNetworkAvailable())
{
    // do network-bound stuff
}
else
{
    // notify the user that there is no network connection
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文