如何在.NET MAUI安卓应用中嵌入exe?权限与脚本使用问题
问题1:在.NET MAUI安卓应用中嵌入并使用exe文件
首先要明确:Windows平台的exe是PE格式二进制文件,无法在安卓(Linux内核)上直接运行,这是核心限制。先解决你遇到的权限与文件复制问题,再说明后续方向:
- 权限问题:安卓应用不能直接写入
Directory.GetCurrentDirectory()指向的位置,必须使用应用的私有可读写目录FileSystem.AppDataDirectory,修改代码如下:
fullPathToHolochainExe = Path.Combine(FileSystem.AppDataDirectory, "HolochainBinaries/beta/holochain.exe"); // 先确保目标目录存在 Directory.CreateDirectory(Path.GetDirectoryName(fullPathToHolochainExe)); if (!File.Exists(fullPathToHolochainExe)) { using (FileStream fsDst = new FileStream(fullPathToHolochainExe, FileMode.CreateNew, FileAccess.Write)) { byte[] bytes = Resources.holochain; fsDst.Write(bytes, 0, bytes.Length); // 安卓是Linux系统,若后续用对应架构的二进制文件,需添加执行权限 var fileInfo = new FileInfo(fullPathToHolochainExe); fileInfo.UnixFileMode = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute; } }
但复制成功后exe依然无法运行,你需要获取Holochain的安卓/ARM架构适配版本(ELF格式二进制文件),而非Windows的exe。
问题2:MAUI安卓应用能否使用PowerShell及管道操作
- 安卓基于Linux内核,没有预装PowerShell,PowerShell是Windows专属的壳程序,无法在安卓上使用。
- 安卓可以使用系统自带的
sh或bash(部分设备预装,也可通过组件安装获取)实现类似管道的操作,针对适配安卓的Holochain二进制文件,有两种传递字符串的方式:- 直接通过进程标准输入传递:
var processStartInfo = new ProcessStartInfo { FileName = fullPathToHolochainAndroidBinary, // 安卓版二进制文件路径 RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true }; using (var process = Process.Start(processStartInfo)) { process.StandardInput.WriteLine("需要传递的字符串"); process.StandardInput.Close(); process.WaitForExit(); }- 通过shell管道命令执行:
var processStartInfo = new ProcessStartInfo { FileName = "/system/bin/sh", Arguments = $"-c \"echo '需要传递的字符串' | {fullPathToHolochainAndroidBinary}\"", UseShellExecute = false, CreateNoWindow = true }; using (var process = Process.Start(processStartInfo)) { process.WaitForExit(); }
内容的提问来源于stack exchange,提问作者David Ellams
相关产品推荐
相关产品推荐

