2017-09-26 6 views
-1

Pour mon projet d'école, je souhaite configurer une connexion via NetSH en utilisant C#. J'ai googlé somethings et est venu avec le code suivant:C# démarrage Ligne de commande 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; 
     } 
    } 
} 

Maintenant, je cours d'abord une instance appelée « processus » (run()) pour configurer la connexion, puis-je utiliser la même méthode et ainsi une nouvelle instance avec le même nom pour une commande de démarrage.

Dans la forme, je fais une nouvelle instance de cette classe (connexion) avec ce code:

 private void btn_startnetwork_Click(object sender, EventArgs e) 
    { 
     connection = new Connection(); 
     connection.Run(); 
     connection.ChangeCommand("wlan start hostednetwork"); 
     connection.Run(); 
    } 

Le problème est: Je ne vois aucune ouverture de programme lorsque je clique sur le bouton. Je sais que j'ai dit que 'CreateNoWindow' devrait être vrai, mais même quand je le mets sur false, il ne démarrera pas netSH. Par conséquent, je ne sais pas si le programme fait ce qu'il devrait faire.

Et je commence un nouveau processus juste pour une autre commande. Ce processus recommence netsh.exe. Je ne sais pas si c'est correct ou non.

+1

votre processus ne démarre jamais. après la ligne avec CreateNoWindow, ajoutez process.Start(); voir aussi: https://msdn.microsoft.com/de-de/library/e8zac0ca(v=vs.110).aspx –

+0

o damn im dumb ... Merci @ ralf.w. Devrais-je le jeter après chaque commande ou non? – Gigitex

+0

à la fin de Run() votre processus est l'histoire ;-) (disposé par .net garbage collector) –

Répondre

1

d'abord, vous devez réécrire 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(); 
    } 

et l'appeler comme ceci:

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

ou encore plus court (votre deuxième appel):

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

avec un opérateur supplémentaire

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

cela ressemble encore plus agréable:

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

Si vous utilisez le constructeur supplémentaire. Que se passe-t-il avec mes propriétés? Comment sait-il l'information? – Gigitex

+1

la partie *: this() * appelle le constructeur par défaut en premier. Si vous voulez changer vos propriétés, vous devez d'abord mettre votre connexion à une variable (Connection first = new Connection(), first.xxx = zzz, first.Run(), etc.) –

+0

Donc pour la dernière partie vous êtes en disant que je dois faire une nouvelle instance pour chaque proces? – Gigitex