0

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());
    }
}

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

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Sam
  • 145
  • 1
  • 1
  • 12

2 Answers2

0

What you're looking for is where Android stores the Shared Preference file for applications that make use of it's default PreferenceManager.

I'd refer to this SO post which answers your question pretty well

SharedPreferences are stored in an xml file in the app data folder, i.e.

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml

or the default preferences at:

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml

SharedPreferences added during runtime are not stored in the Eclipse project.

Note: Accessing /data/data/ requires superuser privileges

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • Hello and thank you really much for your answer. Is there a way to do this in Visual Studio as well? Do you know that? Unfortunately, I used Xamarin.Android for creating my application, and in your linked post, they use Eclipse.^^ – Sam Jul 19 '19 at 09:12
0

A simple method is to use Android Device Monotor,you can open it by clicking Tools--> android-->Android Device Monotor... For example: enter image description here

The path in my device is as follows:

 /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml

And we notice three buttons in the upper right corner of the picture. The first one is used toPull a file from the device,the second one is used to Push a file onto the device,and the last one is used to delete the preferences.xml file.

So we can pull the preferences.xml file from current device to our computer and edit it as we want, and then push the updated preferences.xml to the folder again.Then we will get the value of preferences.xml file .

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Hello and thank you so much for your answer and your help efforts. Unfortunately, I am using Visual Studio - Xamarin.Android ^^ is there a similar way here, too? do you know that? – Sam Jul 19 '19 at 09:07
  • @Sam Yes, I am just talking about Visual studio- Xamarin.Android. – Jessie Zhang -MSFT Jul 19 '19 at 09:13
  • Ahm wait alright okay... sorry xD was just a little confused as I never saw that interface you showed^^ Where can I find ```Tools```? In my Visual Studio menu bar, there is ```File```, ```Edit```, ```View```, ```Project```, ```Build```, ```Debug```, ```Team```, ```Extras```, ```Test```, ```R-Tools```, ```Window``` and ```Help``` – Sam Jul 19 '19 at 09:29
  • what's the version of your VS? Does the R-tools have the option of `android-->Android Device Monotor..`? Tools menu is available in VS2017 and 2019 on my computer. – Jessie Zhang -MSFT Jul 19 '19 at 09:36
  • I am using Visual Studio 2017. ```R-Tools``` seem to be something completely different, the dropdown-menu-items are: ```Drawings```, ```Data```, ```Window```, ```Install R-Client from Microsoft```, ```R-Products of Microsoft```, ```Set Up Remote-R```, ```RTVS-Documentation and Examples```, ```R-Documentation```, ```Feedback```, ```Editor-Options```, ```Options``` and ```Data Science Settings``` – Sam Jul 19 '19 at 10:04
  • Could you please post the screenshot of VS to https://imgur.com/user/iGur and share the link to me ? – Jessie Zhang -MSFT Jul 22 '19 at 09:58
  • Okay, I did, here is the link: https://imgur.com/a/UyfaKZr Unfortunately I installed Visual Studio in German language, so i tried to translate the terms of the menu in the comment above. – Sam Jul 22 '19 at 14:29
  • You can try to reset setting by following link: https://stackoverflow.com/questions/36108515/how-to-reset-settings-in-visual-studio-code – Jessie Zhang -MSFT Jul 24 '19 at 02:23