Question

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 :(

Was it helpful?

Solution

IsolatedStorageFile class has a method called FileExists. See the documentation here If 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.

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

or

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

I will not guarantee that the above code will work exactly, but this is the general idea of how to go about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top