检查WP7内存中是否存在xml文件
我将 xml 文件从互联网下载到内存手机。我想查看互联网连接是否可用于下载,如果没有则发送消息。如果没有,我想查看 xml 文件是否已存在于内存中。如果存在,则应用程序不会进行下载。
问题是我不知道如何创建“if”条件来查看文件是否存在。
我有这个代码:
public MainPage()
{
public MainPage()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
InitializeComponent();
WebClient downloader = new WebClient();
Uri xmlUri = new Uri("http://dl.dropbox.com/u/32613258/file_xml.xml", UriKind.Absolute);
downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded);
downloader.DownloadStringAsync(xmlUri);
}
else
{
MessageBox.Show("The internet connection is not available");
}
}
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();
var stream = new IsolatedStorageFileStream("xml_file.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage);
using (StreamWriter writeFile = new StreamWriter(stream))
{
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.
The problem is i don't know how to make the "if" condition to see if the file exists.
I have this code:
public MainPage()
{
public MainPage()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
InitializeComponent();
WebClient downloader = new WebClient();
Uri xmlUri = new Uri("http://dl.dropbox.com/u/32613258/file_xml.xml", UriKind.Absolute);
downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded);
downloader.DownloadStringAsync(xmlUri);
}
else
{
MessageBox.Show("The internet connection is not available");
}
}
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();
var stream = new IsolatedStorageFileStream("xml_file.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage);
using (StreamWriter writeFile = new StreamWriter(stream))
{
string xml_file = e.Result.ToString();
writeFile.WriteLine(xml_file);
writeFile.Close();
}
}
}
}
I don't know how to see if the file exist with a condition :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
isolatedStorageFile类有一个名为
FileExists
的方法。请参阅此处的文档如果您只想检查文件名,也可以使用
GetFileNames
方法,该方法为您提供isolatedStorage 根目录中文件的文件名列表。 文档在这里。否则
我不保证上面的代码能够工作确实如此,但这是如何进行的总体思路。
IsolatedStorageFile class has a method called
FileExists
. See the documentation hereIf you want to check for just the fileName you can also, use the
GetFileNames
method which gives you the list of file names of files in the root of the IsolatedStorage. Documentation here.or
I will not guarantee that the above code will work exactly, but this is the general idea of how to go about it.