C#启动NetSH命令行

C#启动NetSH命令行,c#,cmd,process,netsh,C#,Cmd,Process,Netsh,对于我的学校项目,我想使用C#通过NetSH建立连接。 我在谷歌上搜索了一些东西,并得出了以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; namespace Server_Smart_Road

对于我的学校项目,我想使用C#通过NetSH建立连接。 我在谷歌上搜索了一些东西,并得出了以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Server_Smart_Road
{
    class Connection
    {
        private string FileName { get; }
        private string Command { get; set; }
        private bool UseShellExecute { get; }
        private bool RedirectStandardOutput { get; }

        private bool CreateNoWindow { get; }

        public Connection()
        {
            FileName = "netsh.exe";
            Command = "wlan set hostednetwork mode=allow ssid=SmartRoad key=smartroad123";
            UseShellExecute = false;
            RedirectStandardOutput = true;
            CreateNoWindow = true;

        }

        public void ChangeCommand(string command)
        {
            Command = command;
        }

        public void Run()
        {
            Process process = new Process();
            process.StartInfo.FileName = FileName;
            process.StartInfo.Arguments = Command;
            process.StartInfo.UseShellExecute = UseShellExecute;
            process.StartInfo.RedirectStandardOutput = RedirectStandardOutput;
            process.StartInfo.CreateNoWindow = CreateNoWindow;
        }
    }
}
现在,我首先运行一个名为“process”(run())的实例来配置连接,然后使用相同的方法,因此使用一个具有相同名称的新实例作为启动命令

在形式中,我使用以下代码创建此类(连接)的新实例:

       private void btn_startnetwork_Click(object sender, EventArgs e)
    {
        connection = new Connection();
        connection.Run();
        connection.ChangeCommand("wlan start hostednetwork");
        connection.Run();
    }
问题是:我没有看到任何程序在 我点击按钮。我知道我说过“CreateNoWindow”应该是真的,但即使我将其设置为false,它也不会启动netSH。因此,我不知道该程序是否做了它应该做的事情


我正在开始一个新的过程,只是为了另一个命令。此进程将再次启动netsh.exe。我不知道这是否正确。

首先,您应该重写Run():

这样称呼它:

connection = new Connection();
connection.Run("wlan set hostednetwork mode=allow ssid=SmartRoad key=smartroad123");
甚至更短(您的第二次通话):

有一个额外的承包商

public Connection(string fn) : this()
{
    FileName = fn;
}
这看起来更漂亮:

new Connection("netsh.exe").Run("wlan set hostednetwork ... ");
new Connection("netsh.exe").Run("wlan start hostednetwork");

你的过程永远不会开始。在CreateNoWindow add process.Start()的行之后;请看:哦,该死的,我是笨蛋。。。谢谢你@ralf.w。我是否应该在每个命令后处理它?在Run()结束时,您的进程是历史;-)(由.net垃圾收集器处理)@ralf.w垃圾收集器处理它不需要更多时间吗?启动netsh.exe需要更多时间吗??或者等待下一次鼠标单击?如果使用其他构造函数。我的财产怎么了?它是如何知道信息的?part*:this()*首先调用默认构造函数。如果要更改属性,必须首先将连接放在变量上(Connection first=new Connection();first.xxx=zzz;first.Run();等等),因此在最后一部分中,你说我需要为每个进程创建一个新实例?你一直在创建新的进程实例。var c=新连接()。。。将创建新的连接实例。
public Connection(string fn) : this()
{
    FileName = fn;
}
new Connection("netsh.exe").Run("wlan set hostednetwork ... ");
new Connection("netsh.exe").Run("wlan start hostednetwork");