【问题标题】:C# starting up NetSH commandlineC# 启动 NetSH 命令行
【发布时间】:2017-09-26 08:59:47
【问题描述】:

对于我的学校项目,我想使用 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' 应该为真,但即使我将它设置为假,它也不会启动netSH。结果,我不知道程序是否做了它应该做的事情。

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

【问题讨论】:

  • 您的进程永远不会启动。在 CreateNoWindow 行之后添加 process.Start();另见:msdn.microsoft.com/de-de/library/e8zac0ca(v=vs.110).aspx
  • 我该死的笨...谢谢@ralf.w。我应该在每个命令之后处理它吗?
  • 在 Run() 结束时,您的进程是历史;-)(由 .net 垃圾收集器处理)
  • @ralf.w 垃圾收集器不需要更多时间来处理它吗?
  • 更多时间作为启动 netsh.exe ??还是等待下一次鼠标点击?

标签: c# cmd process netsh


【解决方案1】:

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

public void Run(string cmd)
    {
        Process process = new Process();
        process.StartInfo.FileName = FileName;
        process.StartInfo.Arguments = cmd;
        process.StartInfo.UseShellExecute = UseShellExecute;
        process.StartInfo.RedirectStandardOutput = RedirectStandardOutput;
        process.StartInfo.CreateNoWindow = CreateNoWindow;
        process.Start();
    }

然后这样称呼它:

connection = new Connection();
connection.Run("wlan set hostednetwork mode=allow ssid=SmartRoad key=smartroad123");

甚至更短(您的第二次通话):

new Connection().Run("wlan start hostednetwork");

有一个额外的构造器

public Connection(string fn) : this()
{
    FileName = fn;
}

这看起来更好:

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

【讨论】:

  • 如果使用附加构造函数。我的财产会怎样?它是如何知道信息的?
  • 部分 * : this()* 首先调用默认构造函数。如果你想改变你的属性,你必须先把你的 Connection 放到一个变量中(Connection first = new Connection(); first.xxx = zzz; first.Run(); etc.)
  • 所以对于最后一部分你说我需要为每个过程创建一个新实例?
  • 您一直在创建新的流程实例。 var c = 新连接(); ... 将创建新的连接实例。
猜你喜欢
  • 2013-08-26
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多