我尝试在用 C# 编写的 Windows 服务中使用特定的 Linux 命令执行 wsl.exe。但是,每次我遇到错误消息(在我的日志文件中):
The file cannot be accessed by the system.
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "wsl.exe",
Arguments = "ls",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
_logger.LogInformation($"Output received: {e.Data}");
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
_logger.LogError($"Error received: {e.Data}");
}
};
process.Exited += (sender, e) =>
{
_logger.LogInformation($"Process exited with code: {process.ExitCode}");
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
我找到了解决方案,只需使用命令
wsl --update
更新 wsl