Sam Mettenmeyer

Sam Mettenmeyer

  • NA
  • 108
  • 10.6k

Write sth into a txt-file & open it with pc afterwards

Jul 26 2019 4:07 AM
I am trying to write some kind of an external config file for my Xamarin.Android-application. Just .txt and this only needs to contain several strings, so no rocket science^^
 
e.g. (Sample Goal/Hopefully final content of the text file:)
  1. [TestSection]  
  2. test=12345 
I tried it with a tutorial and the following code:
  1. //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:  
  2. public void SaveSomethingIntoExternalTextFile(string toWrite)  
  3. {  
  4.     var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "config.txt");  
  5.     using (var writer = System.IO.File.CreateText(backingFile))  
  6.     {  
  7.         writer.WriteLine(toWrite);  
  8.     }  

Via Debugging, I found out that indeed he does write the value, at least I can retrieve it with the following function (I changed the type of the value to integer as in my tutorial (https://docs.microsoft.com/de-de/xamarin/android/platform/files/index#reading-or-writing-to-files-on-internal-storage) it was integer as well):
  1. //This code snippet provides one way to read an int value that was stored in a text file:  
  2. public int ReadSomethingFromTextFile()  
  3. {  
  4.     var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "config.txt");  
  5.   
  6.     if (backingFile == null || !System.IO.File.Exists(backingFile))  
  7.     {  
  8.         return 0;  
  9.     }  
  10.   
  11.     int count = 0;  
  12.     using (var reader = new StreamReader(backingFile, true))  
  13.     {  
  14.         string line;  
  15.         while ((line = reader.ReadLine()) != null)  
  16.         {  
  17.             if (int.TryParse(line, out var newcount))  
  18.             {  
  19.                 count = newcount;  
  20.             }  
  21.         }  
  22.     }  
  23.     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, 
and the only app-specific path I can access via explorer is
Phone/Android/data/com.company_name.application_name/files, 
but this folder seem to be empty, at least no file is shown there (no hidden file as well, I checked this already).

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:
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SaveSomethingIntoExternalTextFile("[TestSection]");  
  5.     SaveSomethingIntoExternalTextFile("test="+12345);  

Can anyone maybe help? What am I doing wrong?

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?