Monday, 27 February 2023

How to solve 'Permission denied error' when creating a Process for .armv8 file in .NET MAUI Android Emulator

I have a .NET MAUI mobile-project and I am trying to call code that has been compiled from C++, namely an .armv8 file. I am trying the Process class to get this to work.

I have tried the following steps:

  1. Save the .armv8-file as an Embedded resource in my project and set Copy to output directory to Copy if newer. The file is in a folder in the mobile-project (so not in de platforms part of the project, but in a folder in the root of the project).

File settings

  1. Request the necessary file-permission (read & write) for Android:
PermissionStatus status = await Permissions.RequestAsync<Permissions.StorageRead>();
        PermissionStatus status2 = await Permissions.RequestAsync<Permissions.StorageWrite>();

These are granted successfully.

  1. Copy the file to the Android specific AppData folder, using the following code:
private string ExtractFiles()
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;

            foreach (var res in assembly.GetManifestResourceNames())
            {
                if (res.Contains(".armv8"))
                {
                    // read source file 
                    using var stream = assembly.GetManifestResourceStream(res);
                    using var reader = new StreamReader(stream);
                    var content = reader.ReadToEnd();

                    // generate target path inside data folder
                    string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, res);

                    // write file into data folder
                    using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
                    using StreamWriter streamWriter = new StreamWriter(outputStream);

                    streamWriter.Write(content);

                    return targetFile;
                }
            }

            return string.Empty;
        }
  1. Tried to change the file-permissions to open with the following code (parameter is the output from step 2):
 public partial void SetPermissionToFile(string path)
        {
            string[] cmd = { "chmod", "744", path };
            Java.Lang.Runtime? runtime = Java.Lang.Runtime.GetRuntime();

            runtime?.Exec(cmd);
        }
  1. Starting a process with the file, with the following code:
_processStartInfo = new ProcessStartInfo
            {
                FileName = path,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };
            _process = new Process { StartInfo = _processStartInfo };
            _process.Start();

When the _process.Start() method gets called, I am getting a Permission denied error, looking like this:

Permission denied exception

Is this because I am using an emulator? Or is the file maybe in the wrong directory for the Process to work with?

I saw a similar post that got it worked out, but that does not seem to work for me yet: https://chess.stackexchange.com/questions/17685/how-to-send-uci-messages-from-c-app-to-stockfish-on-android/17856#17856?newreg=ea938bb4ac3147878e4fdc29626050cc

Thanks in advance for trying to help me :)



from How to solve 'Permission denied error' when creating a Process for .armv8 file in .NET MAUI Android Emulator

No comments:

Post a Comment