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

C# .NET中执行CMD命令arp -a无输出问题求助

解决思路

可能的原因及对应方案:

  • 等待进程执行完毕再读取输出
    部分命令的输出不会立即写入StandardOutput流,直接调用ReadToEnd()可能在进程未完成时就读取到空内容。可以添加p.WaitForExit()确保进程完全执行后再处理输出(ReadToEnd()本身会阻塞直到流结束,但结合WaitForExit()能避免极端场景下的异常)。

  • 检查并读取StandardError流
    arp -a在某些场景下(比如权限不足、系统配置异常)会将内容输出到StandardError而非StandardOutput。需要开启错误流重定向,并读取其内容排查问题:
    修改ProcessStartInfo配置,添加RedirectStandardError = true,然后调用p.StandardError.ReadToEnd()获取错误输出。

  • 以管理员权限运行程序
    部分Windows系统中,arp -a需要管理员权限才能完整输出ARP表信息,而ipconfig无需权限。可以右键程序选择「以管理员身份运行」,或者在项目的app.manifest文件中修改权限配置:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    
  • 指定arp.exe的完整路径
    若系统PATH变量未正确包含arp.exe所在目录(通常为C:\Windows\System32\arp.exe),可以直接使用完整路径启动进程,避免命令找不到的问题:

    new ProcessStartInfo(@"C:\Windows\System32\arp.exe", "-a")
    

修改后的示例代码

Process p = null;
string output = string.Empty;
string errorOutput = string.Empty;

try
{
    p = Process.Start(new ProcessStartInfo(@"C:\Windows\System32\arp.exe", "-a")
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    });

    output = p.StandardOutput.ReadToEnd();
    errorOutput = p.StandardError.ReadToEnd();
    p.WaitForExit();

    Console.WriteLine("标准输出:");
    Console.WriteLine(output);
    Console.WriteLine("错误输出:");
    Console.WriteLine(errorOutput);
    
    p.Close();
}
catch(Exception e)
{
    throw new Exception("Error", e);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 15:45:35