检查内存WP7中是否存在xml文件

时间:2011-10-31 10:38:03

标签: xml windows-phone-7 isolatedstoragefile

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

我不知道如何查看该文件是否存在条件:(

1 个答案:

答案 0 :(得分:5)

IsolatedStorageFile类有一个名为FileExists的方法。查看documentation here 如果您只想检查fileName,也可以使用GetFileNames方法,该方法为您提供IsolatedStorage根目录中文件的文件名列表。 Documentation here.

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if(myIsolatedStorage.FileExists("yourxmlfile.xml))
{
    // do this
}
else
{
    // do that
}

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames = myIsolatedStorage.GetFileNames("*.xml")
foreach (string fileName in fileNames)
{
    if(fileName == "yourxmlfile.xml")
    {
      // do this
    }
    else
    {
      // do that
    }
}

我不保证上面的代码能够正常工作,但这是如何解决它的一般想法。