C#启动NetSH命令行

时间:2017-09-26 08:59:47

标签: c# cmd process netsh

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

library(data.table);

file_1 = read.table(
  header = TRUE,
  text = "
  Month   Sales   Outlet  
  256     24      2453
  256     500     2453
  243     48      2341
  242     47      2356
  243     89      2356"
);

file_2 = read.table(
  header = TRUE,
  text = "
  Month  Price   Salesvolume Outlet  
     256        1840    222700  2453
     256        1840    237600  2453
     243        1840    277100  2341
     242        1840    279200  2356
     243        1840    451400  2356
  "
);

# make files into data tables
file_1 = setDT(x = file_1);
file_2 = setDT(x = file_2);

# merge file_1 to file_2 by "Outlet"
outcome = file_1[file_2, on = "Outlet"];
outcome;

   Month Sales Outlet i.Month Price
1:   256    24   2453     256  1840
2:   256   500   2453     256  1840
3:   256    24   2453     256  1840
4:   256   500   2453     256  1840
5:   243    48   2341     243  1840
6:   242    47   2356     242  1840
7:   243    89   2356     242  1840
8:   242    47   2356     243  1840
9:   243    89   2356     243  1840
   Salesvolume
1:      222700
2:      222700
3:      237600
4:      237600
5:      277100
6:      279200
7:      279200
8:      451400
9:      451400

# then, group this table by "Outlet", and then select only the first element in 
# each group. This is the given by .SD[1] and by = "Outlet", or for each data
# table created in each group, select only the first element. .SD refers to the
# Subsets of Data for each group, excluding "Outlet" (the key), and the [1] is
# an indexing operation to get the first element.
outcome = outcome[, .SD[1], by = "Outlet"]
outcome;

   Outlet Month Sales i.Month Price
1:   2453   256    24     256  1840
2:   2341   243    48     243  1840
3:   2356   242    47     242  1840
   Salesvolume
1:      222700
2:      277100
3:      279200

现在我先运行一个名为'过程' (run())配置连接,然后我使用相同的方法,因此启动命令的同名新实例。

在表单中,我使用以下代码创建此类(Connection)的新实例:

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;
        }
    }
}

问题是:我什么时候看不到任何节目 我点击按钮。我知道我已经说过' CreateNoWindow'应该是真的,但即使我把它设置为假,它也不会启动netSH。因此,我不知道该计划是否应该做它应该做的事情。

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

1 个答案:

答案 0 :(得分:2)

首先,你应该重写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");