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.
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 –
o damn im dumb ... Merci @ ralf.w. Devrais-je le jeter après chaque commande ou non? – Gigitex
à la fin de Run() votre processus est l'histoire ;-) (disposé par .net garbage collector) –