0

I am trying to merge some txt-Files from a folder in a Xamarin Form app.

The path is selectable by the user (i´ve created a simple folder browser for that).

e.g. the source path is

/storage/1EE7-170F/

I collect e.g. all txt-files in that folder, read them using File.ReadAllText, do some processing. That is working fine so far.

I also want to write the results to a new file in the same folder using File.WriteAllText. Target file would be:

/storage/1EE7-170F/SomeResultFile.txt

Getting the directories content is in AppName.Android:

var fileContents = Directory.GetFiles(baseDirectory.FullPath, searchPattern)
                    .Select(fullPath =>
                    {
                        var fileName = Path.GetFileName(fullPath);
                        var extension = Path.GetExtension(fullPath);
                        return new FileContent(fileName, fullPath, extension);
                    });

Reading of the files goes like this in common project:

var content = File.ReadAllText(fileContent.FullPath);

The files are written like this (also in common project):

var baseDir = Path.GetDirectoryName(sourceFiles.First().FullPath);
var targetFilePath = Path.Combine(baseDir, filename); 
File.WriteAllLines(targetFilePath, lines);

But i always get an UnauthorizedAccessException saying 'Access to the path "/storage/1EE7-170F/SomeResultFile.txt" is denied.'

I wonder why i can read from that folder, but not write to it.

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.

I have both permission "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE" enabled in the manifest.

In MainActivity.OnCreate i have added the following:

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

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...

Any ideas what could be the problem?

Thanks a lot for help!

emvoll
  • 99
  • 7
  • Im pretty sure you can only write to the internal data directory of your app. See https://stackoverflow.com/questions/44587187/android-how-to-write-a-file-to-internal-storage – heinst Feb 18 '20 at 17:56
  • Well, writing to a S10 internal Documents folder doesn't work either... – emvoll Feb 18 '20 at 18:22
  • it would be helpful to see how you are writing the file then – heinst Feb 18 '20 at 19:03
  • Sure! I´ve added the code snippets to the original question for better readability... – emvoll Feb 18 '20 at 19:16
  • If you want to wirte file to document folder, you should use `documents provider` to achieve that. https://developer.android.com/training/data-storage/shared/documents-files But, I suggest you to copy the file to the Download folder(`Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;`). Use `FileStream` to wirte it. – Leon Feb 19 '20 at 06:17
  • Update: I got it working by choosing a different folder. So i think i´ll restrict the chosen folder to that kind of folders that are working. Thanks for all of your suggestions and hints! – emvoll Feb 19 '20 at 07:01
  • I am gold to hear that you solved this issue, you are welcome. – Leon Feb 20 '20 at 06:36

0 Answers0