Bug command dos WPF

Résolu
pl4hs Messages postés 53 Date d'inscription dimanche 17 janvier 2010 Statut Membre Dernière intervention 26 avril 2011 - 8 mars 2011 à 18:15
pl4hs Messages postés 53 Date d'inscription dimanche 17 janvier 2010 Statut Membre Dernière intervention 26 avril 2011 - 8 mars 2011 à 18:36
Bonjour a tous,

Voila mon problème, je suis sur un petit projet perso(WPF), sur d'ancien projet(Window from), j'ai fait des dizaine de fonction qui exécuter des commande dos, mais la je n'y arrive pas et je comprend pas d'ou viens le bug.

Voila le principe,tout simple : Récupérer le texte d'un textbox (commande) est l’exécuter en console.

voila mon code :

Event dans mon main:

        private void m_button_ExecCmd_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(libDos.execCMD(m_textBox_DosCmd.Text));
        }
(Message box ici juste pour la recherche de bug le temps du développement).


Fonction dans ma dll :
public String execCMD(string command)
        {
            string result = string.Empty;
            try
            {
                Process p = new Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = command;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                result = p.StandardOutput.ReadToEnd();
                p.Close();
            }
            catch (Exception e)
            {
                result = e.ToString();
            }
            return result;
        }


La, le souci est que les commande marche a condition qu'il n'y est pas d'espace.(ipconfig,..), si je tape des commande avec espace(ping XXX.fr, tracert XXX,..) il me généré une exception .

Si quelqu'un peut m’éclairer sur mon problème..

En vous remerciant..

Cordialement

2 réponses

pl4hs Messages postés 53 Date d'inscription dimanche 17 janvier 2010 Statut Membre Dernière intervention 26 avril 2011
8 mars 2011 à 18:36
Probleme résolu :
        public String execCMD(string command)
        {
            string result = string.Empty;
            try
            {
                System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                procStartInfo.CreateNoWindow = true;
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                result = proc.StandardOutput.ReadToEnd();
            }
            catch (Exception e)
            {
                result = e.ToString();
            }
            return result;
        }


Merci a toi
3
cs_jopop Messages postés 1540 Date d'inscription lundi 26 mai 2003 Statut Membre Dernière intervention 1 août 2013 12
8 mars 2011 à 18:22
Salut,

Je préviens de suite, je suis pas sûr de mon coup ^^
Quand tu as des espaces c'est qu'il s'agit d'arguments de la commande. Essaie donc un truc comac :

public string execCMD(string command, string arguments) {
[..]
p.StartInfo.FileName = command;
p.StartInfo.Arguments = arguments;
[..]
}


Si tu tiens absolument à n'avoir qu'une zone de saisie, il faudra alors toi-même faire les 2 substring autour du premier espace de la chaîne entrée par l'utilisateur.

En espérant avoir répondu à ta question
0
Rejoignez-nous