【问题标题】:Why cant I start a process without using ShellExecute set to true?为什么我不能在不使用 ShellExecute 设置为 true 的情况下启动进程?
【发布时间】:2026-01-30 11:00:02
【问题描述】:

所以我目前正在尝试从我的 ASP.NET Core MVC WebApp 启动命令提示符窗口,当我单击一个按钮时它在调试模式下运行,但由于某种原因它不会启动,除非我指定 UseShellExecute 和将其设置为 True。

这是一个问题,因为如果我将其设置为 True,我将无法重定向标准输入,这是一件坏事。 我需要能够做到这一点。

这里是代码

public static void Start(string dirPath)
{
    Process serverProcess = new Process
    {
        StartInfo =
        {
            CreateNoWindow = false,
            FileName = "cmd.exe",
            Arguments = "",
            WindowStyle = ProcessWindowStyle.Maximized,
            RedirectStandardInput = true
        }
    };   

    serverProcess.Start()
}

它确实会调用代码,因为请记住,当我执行 UseShellExecute = True 时它会起作用,并且它还会遇到断点等。

如何在不设置 UseShellExecute = True 的情况下启动 cmd 或特定批处理文件?

【问题讨论】:

  • 可能是因为 cmd.exe 不在工作文件夹中。
  • 嗯,使用参数来执行你需要的东西就可以了。
  • 我尝试将工作目录更改为 CMD 所在的 System32 文件夹,但无法启动
  • 另外,我可以运行 Notepad.exe 但不能运行 cmd.exe

标签: c# asp.net .net asp.net-core .net-core


【解决方案1】:

UseShellExecute 对于 Core 默认为 false。如果您不想设置 UseShellExecute 。尝试在下面设置Arguments 作为解决方法:

Arguments = $"/c start cmd.exe", 

参考:https://github.com/dotnet/corefx/issues/21767

【讨论】:

  • 我似乎无法将输入重定向到 cmd,因为它没有连接到我创建的进程
最近更新 更多