确定互联网连接是否可用
)我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NetworkInterface.GetIsNetworkAvailable() 方法可以告诉您:
The NetworkInterface.GetIsNetworkAvailable() method can tell you this: