Question

I have an XML setting file for my android application. When application is published it has some default values that have to published with app. And after changing settings by user the value of this setting file has to be changed. I use an XML asset file to implement this feature. This file has some default values. And by publishing an app, it's also copied in device. But when I write to this file and come back it seems that the file was never changed. I use a file manager and look to my file on the device and see that this file had never been changed and have it has default values. I'm using monondroid and this is the code that I use for writing in my xmlsetting file:

public bool writeToFile (string newXmlContent)
        {
            try {
                Stream sw= Application .Context .OpenFileOutput (AppConstants .xmlSettingFileName ,
                                                                 FileCreationMode .Private  );
                StreamWriter stw= new StreamWriter (sw);
                stw.Write (newXmlContent );
                stw.Flush ();
                XmlContent = newXmlContent ;
                stw.Close ();
                sw=null;
                return true;

            } catch (Exception ex) {
                Common .HandleException (ex);
                return false ;
            }

        }

What's wrong with this?

Was it helpful?

Solution 2

I found that to implementing setting page and using settings for application the best way is using from Preferences.for more information see:

developer.android.com/design/patterns/settings.html

developer.android.com/guide/topics/ui/settings.html

but if any body wants to implement a config file.can use assets for this propose.to writing and reading in asset file must override application class and copy the asset file in oncereate of this class in sd or device memory.after this you can work with this file.

OTHER TIPS

Any particular reason not to use the inbuilt facility for this?

I retrieve any changing persistence values like this:

var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);  
var somePref = prefs.GetString("PrefName", null);

To store them:

var prefEditor = prefs.Edit();
prefEditor.PutString("PrefName", "Some value");
prefEditor.Commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top