save configuration on windows phone in text file

How can we save configuration in text file on windows phone?
how to view the saved config file on phone?
RETAILREALMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PawełI Design & Develop SoftwareCommented:
The first question you should ask your self is do you need to save it in a file? the reason i ask this is because winRT apps have something called a local settings dictionary that lets you store value key pairs with very little effort
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

// save a value
localSettings.Values["mydatakey"] = "my data";

// load a value
Object value = localSettings.Values["mydatakey"];

if (value != null)
{
   var myData = value.ToString();
   //do something with myData

}

// delete value
localSettings.Values.Remove("mydatakey");

Open in new window

now if you need to save something more complex then just key value pairs, we'll then what i would do is create an class and serialize it to a file.

first you need to pick a serialization strategy, commonly you can use Microsoft's Json or xml data contract serializers, to pull this off you need to create your class and decorate it with the DataContract attributes:
using System;
using System.Runtime.Serialization;

namespace serialize_data
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        public DateTime BirthDate { get; set; }

        public Person() { }

    }
}

Open in new window

the quick take away is you need to decorate your class with the DataContract Attribute, each property to be serialized needs to be decorated as a DataMember and you require a parameters constructor. so from the snippet you can see that only the first and last names will be serialized.

Now that you have your "storageClass" set up you have to serialize it to a Storage file
private async void Button_Click(object sender, RoutedEventArgs e)
{
    var pIn = new Person { 
        FirstName = "Pawel", 
        LastName = "Chooch", 
        BirthDate = new DateTime(1984, 01, 01) 
    };

    //serialize using json
    StorageFolder sFolder = await Package.Current.InstalledLocation.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
    StorageFile sFile = await sFolder.CreateFileAsync("fileName.dat", CreationCollisionOption.GenerateUniqueName);

    using (var s = await sFile.OpenStreamForWriteAsync())
    {
        var jsonSerializer = new DataContractJsonSerializer(typeof(Person));
        jsonSerializer.WriteObject(s, pIn);
    }

    //deserialize 
    Person pOut;
    sFolder = await Package.Current.InstalledLocation.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
    sFile = await sFolder.GetFileAsync("fileName.dat");

    using (var s = await sFile.OpenStreamForReadAsync())
    {
        var jsonSerializer = new DataContractJsonSerializer(typeof(Person));
        pOut = jsonSerializer.ReadObject(s) as Person;
    }
}

Open in new window

you'll also need to use

And that's basically it, now one thing you may want to look into is a nuget package called json.net you can go to http://json.net, it's more efficient and more widely used than dataContracts
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Phone

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.