jpm*_*pmd 1 xml isolatedstoragefile windows-phone-7
我将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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何查看该文件是否存在条件:(
IsolatedStorageFile类有一个名为的方法FileExists.请参阅此处的文档
如果您只想检查fileName,也可以使用GetFileNames方法,该方法为您提供IsolatedStorage根目录中文件的文件名列表.文档在这里.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if(myIsolatedStorage.FileExists("yourxmlfile.xml))
{
// do this
}
else
{
// do that
}
Run Code Online (Sandbox Code Playgroud)
要么
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames = myIsolatedStorage.GetFileNames("*.xml")
foreach (string fileName in fileNames)
{
if(fileName == "yourxmlfile.xml")
{
// do this
}
else
{
// do that
}
}
Run Code Online (Sandbox Code Playgroud)
我不保证上面的代码会完全正常工作,但这是如何解决它的一般想法.