2017-09-26 15 views
-1

私の学校のプロジェクトでは、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"); 

+1

あなたのプロセスを開始することはありません。 CreateNoWindowの行の後にprocess.Start()を追加します。以下を参照してください:https://msdn.microsoft.com/de-de/library/e8zac0ca(v=vs.110).aspx –

+0

あなたは私のことを気にしています...ありがとう@ ralf.w。各コマンドの後にそれを破棄すべきかどうか? – Gigitex

+0

あなたのプロセスは履歴です;-)(.netガベージコレクタによって廃棄されました) –

答えて

1

まず、あなたは、ファイル名を指定して実行()書き換える必要があります追加コンストラクタ

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

これも、よりよいなります

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

追加コンストラクタを使用する場合。私の所有物はどうなりますか?どのように情報を知っていますか? – Gigitex

+1

部分*:this()*は最初にデフォルトのコンストラクタを呼び出します。あなたのプロパティを変更したい場合は、Connectionを最初に変数に設定する必要があります(Connection first = new Connection(); first.xxx = zzz; first.Run();など) –

+0

最後の部分はuです私は各プロシーズのために新しいインスタンスを作る必要があると言っていますか? – Gigitex

関連する問題