如何从用 C# 编写的 Windows 服务运行可执行文件?

问题描述 投票:0回答:1

我尝试在用 C# 编写的 Windows 服务中使用特定的 Linux 命令执行 wsl.exe。但是,每次我遇到错误消息(在我的日志文件中):

The file cannot be accessed by the system.

我已使用 LocalSystem 用户和我的个人用户帐户安装并运行该服务,但它不起作用。奇怪的是,当我在 IDE 中运行代码并在服务中设置相同的个人用户帐户时,它运行得很好。我还提供了 wsl 可执行文件的完整路径,但问题仍然存在。总之,当代码由服务作为本地系统或我的个人用户运行时,它不起作用。然而,当我自己运行该程序时,它可以无缝运行。感谢您阅读并帮助我!
下面是我的 Windows 服务正在运行的代码:

        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();
c# windows service windows-subsystem-for-linux
1个回答
0
投票

我找到了解决方案,只需使用命令

wsl --update

更新 wsl
© www.soinside.com 2019 - 2024. All rights reserved.