私の学校のプロジェクトでは、NetSH経由でC#を使用して接続を設定したいと考えています。 私は代をGoogleで検索し、次のコードを思い付いています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;
}
}
}
は今、私は最初の「プロセス」と呼ばれるインスタンスを実行(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を再度開始します。これが正しいかどうかわかりません。
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");
またはより短い(2番目の呼び出し):と
new Connection().Run("wlan start hostednetwork");
あなたのプロセスを開始することはありません。 CreateNoWindowの行の後にprocess.Start()を追加します。以下を参照してください:https://msdn.microsoft.com/de-de/library/e8zac0ca(v=vs.110).aspx –
あなたは私のことを気にしています...ありがとう@ ralf.w。各コマンドの後にそれを破棄すべきかどうか? – Gigitex
あなたのプロセスは履歴です;-)(.netガベージコレクタによって廃棄されました) –