You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL调试时,.NET Core 6控制台应用无法用Process.Start启动另一个应用

在WSL调试环境中通过.NET Core 6控制台应用启动另一个控制台应用的问题

在Visual Studio 2022中调试WSL(Windows Subsystem for Linux)环境下的.NET Core 6控制台应用时,遇到无法通过Process.Start()启动另一个控制台应用的问题,具体情况如下:

最小复现场景

第一个控制台应用代码

static void Main(string[] args)
{
    Console.WriteLine("Hello, World!");
    Process process = new Process();
    process.StartInfo.FileName = Path.Combine(".", "ConsoleApp2");
    using (process)
    {
        process.Start();
        process.WaitForExit();
    }
    Console.ReadLine();
}

第二个控制台应用代码(最简)

static void Main(string[] args)
{
    Console.WriteLine("Hello, World!");
}

第一次尝试的异常

调试时抛出如下异常:

System.ComponentModel.Win32Exception
  HResult=0x80004005
  Message=An error occurred trying to start process './ConsoleApp2' with working directory '/mnt/c/Temp/TestProject2/ConsoleApp1/ConsoleApp1/bin/Debug/net6.0'. No such file or directory
  Source=System.Diagnostics.Process
  StackTrace: (is skiped for brevity)

尝试指定DLL路径的情况

修改代码,尝试直接启动DLL文件:

string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
process.StartInfo.FileName = Path.Combine(path, "ConsoleApp2.dll");

此时抛出异常:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

特殊现象:启动EXE文件的情况

若指定路径为ConsoleApp2.exe,代码如下:

static void Main(string[] args)
{
    Console.WriteLine("Hello, World!");
    Process process = new Process();
    string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    process.StartInfo.FileName = Path.Combine(path, "ConsoleApp2.exe");
    using (process)
    {
        process.Start();
        process.WaitForExit();
    }
    Console.ReadLine();
}

第二个控制台应用代码:

static void Main(string[] args)
{
    Console.WriteLine("The second console!");

    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        Console.WriteLine("The second console: I'm on Windows:\r\n");
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        Console.WriteLine("The second console: I'm on Linux:\r\n");
}

此时第二个应用可运行,但输出显示其处于Windows环境:

Hello, World!
The second console!
The second console: I'm on Windows:

求助需求

已知可通过引用库替代该场景,但因项目需求必须保留当前方案。请问如何在WSL中通过Visual Studio调试时,让一个.NET Core 6控制台应用成功启动另一个控制台应用,且第二个应用运行在WSL环境中?


内容的提问来源于stack exchange,提问作者RomanGirin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 23:50:18