Forum Xamarin.Android

Access Shared Preferences externally / Store a value into a new file / access it externally

TBMSamTBMSam Member ✭✭
edited July 2019 in Xamarin.Android

Hello altogether,

I have the two following methods and I am using them to store a special value locally and be able to access it on application restart:

//Store value locally

    private void SaveSet(string key, string value)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);

        ISharedPreferencesEditor prefEditor = prefs.Edit();

        prefEditor.PutString(key, value);
        // editor.Commit();    // applies changes synchronously on older APIs
        prefEditor.Apply();        // applies changes asynchronously on newer APIs

    }

//Read it again

    private string RetrieveSet(string key)
    {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        return prefs.GetString(key, null);
    }

This works perfectly. Now is it possible to access and edit this Shared Preferences externally? Unfortunately, I cannot find any file when searching in folder

Phone\Android\data\com.<company_name>.<application_name>\files

nor anywhere else. I want / try to edit this value from my computer, after connecting the phone to it. Is this possible?

Alternatively: Can anyone maybe show me how to create a new file in the given path above, write/read it programmatically and how it stays there, even if application is closed / started again? So I can then edit this file with my computer anyhow?

I tried it with the following code, but unfortunately it doesn't work / no file is created or at least i cannot see it in the given path above:

    //"This code snippet is one example of writing an integer to a UTF-8 text file to the internal storage directory of an application:"
    public void SaveValueIntoNewFile(int value)
    {
        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "newFile.txt");
        using (var writer = System.IO.File.CreateText(backingFile))
        {
            writer.WriteLine(value.ToString());
        }
    }

Sorry for my bad language, hope you can nevertheless understand and help me anyhow.

Would be very happy about every answer, thanks in advance and best regards

Best Answer

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Based on my research, nope, I can not found a way to change the value of sharepreference from the comptuer. Changing the value by other application. it is acceptable.

  • TBMSamTBMSam Member ✭✭

    Hello and thank you so much for your answer and your research. What do you mean by

    @LeonLu said:
    Changing the value by other application. it is acceptable.

    If i write another application only for manipulating these values in SharedPreferences, this would be a possible way? As I tried to point on in the headline, I wanted to access them externally. So where does this application then have to run on?

Sign In or Register to comment.