c# - 未找到以下命令 netsh

标签 c# netsh

我正在尝试执行几个“netsh”命令,但无论我做什么,我都会收到相同的错误“找不到以下命令 netsh”。

我可以验证路径“C:\Windows\System32\netsh.exe”是否有效,并且当我通过命令提示符使用相同的参数集运行相同的命令时;一切正常。

这是我正在使用的代码示例。

ProcessStartInfo procInfo = new ProcessStartInfo
            {
                WorkingDirectory = System.IO.Path.GetPathRoot(System.Environment.SystemDirectory),
                FileName = @"netsh.exe",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                Arguments = String.Format(@"{0}\{1} {2}", System.Environment.SystemDirectory, @"netsh.exe", "wlan start hostednetwork"),
                WindowStyle = ProcessWindowStyle.Hidden
            };

            Process proc = Process.Start(procInfo);
            proc.WaitForExit();

最佳答案

您当前正在参数 中传递完整路径名,而我怀疑您希望将其作为文件名 传递。例如:

FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
Arguments = "wlan start hostednetwork"

例如,这个简短但完整的程序不会抛出任何异常 - 我不知道它是否按照您的要求执行,但它不会因您描述的异常而失败:

using System;
using System.Diagnostics;
using System.IO;

class Test
{    
    static void Main()
    {
        ProcessStartInfo procInfo = new ProcessStartInfo
        {
            WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory),
            FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
            Arguments = "wlan start hostednetwork",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden
        };

        Process proc = Process.Start(procInfo);
        proc.WaitForExit();
    }
}

(请注意,当您利用 using 指令而不是使用完全限定名称时,代码如何变得不那么困惑。)

关于c# - 未找到以下命令 netsh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642370/

相关文章:

javascript - Unity C# 如何正确加载托管 DLL?

Java代码检查防火墙的状态

windows-8 - 更改无线 Windows 8 托管网络的 channel

c# - 用于解析由括号分隔的数据的正则表达式

c# - 生成Youtube共享链接

c# - 遍历数据库表的记录集

c# - 从存储过程返回最后插入行的标识

c# - 在 C# 中以编程方式创建 DHCP 预留

http - netsh http 添加 urlacl : add reservation for a group