user2799350
user2799350

Reputation: 399

How to add an existing file to IsolatedStorage at development time?

I have an xml file contains some predefined names. I want it to be in the IsolatedStorage when I execute the app.

I know how to create and save data to IsolatedStorage but how to copy/paste an existing file visually?

IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
isolatedStorage.FileExists(filePath)

I think that adding a file to resources will cause loading the whole file at run time, and I don't want this. Am I right?

Upvotes: 1

Views: 2282

Answers (2)

Mani
Mani

Reputation: 1374

From your question i can understand that your file should be present in the isolated storage when it launches. What i ask you is that where it will be getting that file in your app?

So obviously you need to add to the application resources.Right click on solution explorer and add the xml file. Change the file binding from content to resource.

Use the following code to get the data from xml file.

StreamResourceInfo strm = Application.GetResourceStream(new Uri("/NewApp;component/Sources/employees.xml", UriKind.Relative));
StreamReader reader = new StreamReader(strm.Stream);
string data = reader.ReadToEnd();

In this code i assumed that NewApp is my app name and i kept the employees xml file in Sources folder i created. I have read the data of the xml file into the string data variable.

If you are so strict about keeping it to IsolatedStorage then you can check whether storage contains this file and if doesnt add the file to storage or else you can load it from storage only.

using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (storage.FileExists("employees.xml"))
            {
                //Load it
            }
            else
            {
                System.Windows.Resources.StreamResourceInfo strm = Application.GetResourceStream(new Uri("/NewApp;component/Sources/employees.xml", UriKind.Relative));
                System.IO.StreamReader reader = new System.IO.StreamReader(strm.Stream);
                string data = reader.ReadToEnd();
                using (var file = storage.OpenFile("employees.xml", System.IO.FileMode.Create))
                {
                    using (var writer = new System.IO.StreamWriter(file))
                    {
                        writer.WriteLine(data);
                    }
                }
            }
        }

Upvotes: 2

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

If what you want is copy a file to your own device isolated storage for testing purpose you can use Windows Phone IsoStoreSpy. It provide a interface to be able to view and modify the file thaat are currently in the isolated storage of your app on your device.

If what you want is just have your xml file ship with your xap and don't plan to modify it, just copy it in the project and set the build action of the file to content.

Finally if what you want is have your xml file ship with your xap and then copy it to isostorage at runtime so that you can modify it then you can follow copy the extension method from here and follow the instruction.

Upvotes: 2

Related Questions