我有一个 dotnet core 3.1 Web 应用程序,它获取文件路径和 exe 并运行它,但由于想要利用跨平台功能而切换到 dotnet core。
应用程序正在基于 Windows 的环境中运行,Proocess.Start()我尝试更改环境变量的扩展名。
窗口代码:
var process = await Task.Run(() =>
Process.Start($"{path}/{file}");
Run Code Online (Sandbox Code Playgroud)
当在 Linux 机器上运行时,System.ComponentModel.Win32Exception (8): Exec format error
如果我将文件更改为无法运行的.dll扩展名,我不认为 Wine 是一个选项。.exe
我如何通过 C# 实现这一点,或者是否需要调用包装脚本?
更新
exe 和 dll 都是使用相同的 3.1 从 dotnet core 应用程序构建的,当前使用的代码如下:
var information = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = Path.Join(this.path, this.file),
};
var process = await Task.Run(() =>
Process.Start(information));
Run Code Online (Sandbox Code Playgroud)
我在 Linux 机器上遇到的错误:
System.ComponentModel.Win32Exception(8):System.Diagnostics.Process.ForkAndExecProcess(字符串文件名,字符串[] argv,字符串[] envp,字符串cwd,布尔redirectStdin,布尔redirectStdout,布尔redirectStderr,布尔setCredentials,UInt32执行格式错误userId、UInt32 groupId、UInt32[] groups、Int32& stdinFd、Int32& stdoutFd、Int32& stderrFd、布尔 useTerminal、布尔 throwOnNoExec)
您拥有的 Exe 文件可能是 Windows 可执行文件,因此无法在 Linux 上运行。
您拥有的 .dll 文件不是可执行文件,因此您不能直接启动它Process.Start。但是,它可以通过“dotnet”应用程序启动(假设它有入口点),如果您安装了 .NET core 框架,该应用程序应该已经在您的 Linux 服务器上的路径中。如果路径中不存在 - 它通常位于 /usr/bin/dotnet。如果未安装 .NET core -请先安装它。然后.dll 可以通过以下方式运行:
dotnet PathToYour.dll
Run Code Online (Sandbox Code Playgroud)
然后你的代码就变成:
var information = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = "dotnet",
Arguments = Path.Join(this.path, this.file)
};
Run Code Online (Sandbox Code Playgroud)
如果您有要传递的参数,请将它们附加在 dll 路径之后:
Arguments = Path.Join(this.path, this.file) + " " + "your arguments here"
Run Code Online (Sandbox Code Playgroud)
请注意,假设目标计算机上安装了 .NET Core 框架,您也可以在 Windows 上以相同的方式运行 .dll,因为“dotnet.exe”也存在于 Windows 上并且工作原理相同。