title: Writing to "Documents" folder on Android from Xamarin Forms app
body: <p>I am trying to merge some txt-Files from a folder in a Xamarin Form app.</p>
  
  <p>The path is selectable by the user (i´ve created a simple folder browser for that).</p>
  
  <p>e.g. the source path is</p>
  
  <pre><code>/storage/1EE7-170F/
  </code></pre>
  
  <p>I collect e.g. all txt-files in that folder, read them using File.ReadAllText, do some processing.
  That is working fine so far.</p>
  
  <p>I also want to write the results to a new file in the same folder using File.WriteAllText. Target file would be:</p>
  
  <pre><code>/storage/1EE7-170F/SomeResultFile.txt
  </code></pre>
  
  <p>Getting the directories content is in AppName.Android:</p>
  
  <pre><code>var fileContents = Directory.GetFiles(baseDirectory.FullPath, searchPattern)
                      .Select(fullPath =&gt;
                      {
                          var fileName = Path.GetFileName(fullPath);
                          var extension = Path.GetExtension(fullPath);
                          return new FileContent(fileName, fullPath, extension);
                      });
  </code></pre>
  
  <p>Reading of the files goes like this in common project:</p>
  
  <pre><code>var content = File.ReadAllText(fileContent.FullPath);
  </code></pre>
  
  <p>The files are written like this (also in common project):</p>
  
  <pre><code>var baseDir = Path.GetDirectoryName(sourceFiles.First().FullPath);
  var targetFilePath = Path.Combine(baseDir, filename); 
  File.WriteAllLines(targetFilePath, lines);
  </code></pre>
  
  <p>But i always get an UnauthorizedAccessException saying 'Access to the path "/storage/1EE7-170F/SomeResultFile.txt" is denied.'</p>
  
  <p>I wonder why i can read from that folder, but not write to it.</p>
  
  <p>Writing to System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) is working fine, but that´s not what i need. 
  I need the result files in the same directory as the source files.</p>
  
  <p>I have both permission "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE" enabled in the manifest.</p>
  
  <p>In MainActivity.OnCreate i have added the following:</p>
  
  <pre><code>if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
  {
      ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 0);
  }
  
  if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
  {
      ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 0);
  }
  </code></pre>
  
  <p>I´ve tried that both on Android emulator and a S10 with latest Android installed (on S10 i selected the "Documents" folder). Same result on both systems with different folders...</p>
  
  <p>Any ideas what could be the problem?</p>
  
  <p>Thanks a lot for help!</p>