e.g. (Sample Goal/Hopefully final content of the text file:)
- [TestSection]
- test=12345
- //This code snippet is one example of writing a string to a UTF-8 text file and into the internal storage directory of an application:
- public void SaveSomethingIntoExternalTextFile(string toWrite)
- {
- var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "config.txt");
- using (var writer = System.IO.File.CreateText(backingFile))
- {
- writer.WriteLine(toWrite);
- }
- }
- //This code snippet provides one way to read an int value that was stored in a text file:
- public int ReadSomethingFromTextFile()
- {
- var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "config.txt");
- if (backingFile == null || !System.IO.File.Exists(backingFile))
- {
- return 0;
- }
- int count = 0;
- using (var reader = new StreamReader(backingFile, true))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- if (int.TryParse(line, out var newcount))
- {
- count = newcount;
- }
- }
- }
- return count;
- }
Nevertheless, I cannot find a file anywhere on the mobile phone. And even if I create a new file with the computer in the given path,
name it as written (config.txt) and run the given code again, still
nothing happens, so no data is added to that file. Is the Path
System.Environment.SpecialFolder.Personal
the right one? Or does this point to root directory, where I am always seeking in some kind of user folder?
Because when looking with the debugger what's inside backingFile, it's
/data/data/com.company_name.application_name/files/config.txt,
Phone/Android/data/com.company_name.application_name /files,
I read something about having to root my phone in order to be able to access this path? Makes no sense right?
Isn't there some kind of shared folder, whichs accessable not only by application/operating system but also from outside?
Just for the sake of completeness, my test application is pretty simple:
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SaveSomethingIntoExternalTextFile("[TestSection]");
- SaveSomethingIntoExternalTextFile("test="+12345);
- }
Would be really happy about every answer, thanks in advance and
Best regards
P.S.: If I open the by-computer-generated text-file, even if in the file directory its displayed correctly, the heading of the file says config[1].txt, opening again config[2].txt and so on. Or does that not matter/has nothing to do with my attempt above?
Replies
Know the answer? Post it — somebody with the same question will find it here.