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

直接运行DisplaySwitch 4正常,C#启动CMD执行该命令却报错?

问题原因及解决方案

问题原因

  • 环境变量PATH不完整:直接打开CMD时,系统会加载完整的用户与系统环境变量,其中包含C:\Windows\System32(DisplaySwitch.exe的存放目录)。但在C#代码中设置UseShellExecute = false时,新启动的cmd.exe进程可能未继承完整的系统PATH,导致无法定位DisplaySwitch命令。
  • 32位/64位系统重定向问题:若你的C#程序编译为32位(x86)并运行在64位Windows系统上,系统会自动将对System32目录的访问重定向到SysWOW64目录(存放32位系统文件),而DisplaySwitch.exe仅以64位版本存在于System32中,因此32位进程无法找到该程序。

解决方案

方案1:直接调用DisplaySwitch.exe(推荐)

无需通过cmd.exe中转,直接启动目标程序并传入参数:

ProcessStartInfo processStartInfo = new ProcessStartInfo
{
    FileName = "DisplaySwitch.exe",
    Arguments = "4",
    UseShellExecute = false,
    CreateNoWindow = true
};

using (Process process = Process.Start(processStartInfo))
{
    process.WaitForExit();
}

方案2:通过cmd.exe运行时指定完整路径

在输入命令时,直接使用DisplaySwitch.exe的完整路径:

// 修改输入的命令行内容
process.StandardInput.WriteLine(@"C:\Windows\System32\DisplaySwitch.exe 4");

方案3:解决32位程序的重定向问题

如果程序必须以32位运行,可通过以下两种方式处理:

  • 编译设置调整:在项目属性中,将目标平台改为x64或AnyCPU(并取消勾选“首选32位”选项)。
  • 代码中禁用重定向:
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    
        static void Main()
        {
            IntPtr ptr = IntPtr.Zero;
            try
            {
                // 禁用64位系统的文件重定向
                Wow64DisableWow64FsRedirection(ref ptr);
    
                ProcessStartInfo processStartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = @"C:\Users\MyUsername",
                };
    
                using (Process process = new Process { StartInfo = processStartInfo })
                {
                    process.Start();
                    process.StandardInput.WriteLine("DisplaySwitch 4");
                    process.StandardInput.Flush();
                    process.StandardInput.Close();
    
                    string output = process.StandardOutput.ReadToEnd();
                    string error = process.StandardError.ReadToEnd();
                    process.WaitForExit();
    
                    Console.WriteLine("Output:\n{0}", output);
                    Console.WriteLine("\nError:\n{0}", error);
                }
            }
            finally
            {
                // 恢复重定向
                if (ptr != IntPtr.Zero)
                    Wow64RevertWow64FsRedirection(ptr);
            }
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 17:03:24