IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

[Process] - Récupérer la sortie


Sujet :

C#

  1. #1
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut [Process] - Récupérer la sortie
    Bonjour,

    Je cherche à récupérer la sortie d'un processus voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
                    ProcessStartInfo process = new ProcessStartInfo();
                    process.RedirectStandardOutput = true;
                    process.CreateNoWindow = true;
                    process.UseShellExecute = false;
                    process.FileName = @"flv.exe";
                    process.Arguments = args;
                    p = Process.Start(process);
                    string s = p.StandardOutput.ReadToEnd();  
                    //p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                    //p.BeginOutputReadLine();
                    p.WaitForExit();
    mais ma string s me renvoie une chaîne vide !! Alors que si j'exécute mon programme en mode console il affiche pleeeiin de ligne !! Quelqu'un peut il m'aider ? merci d'avance !

  2. #2
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Tu es parti à l'envers je crois bien.

    Regarde sur cette page en bas y'a un exemple de code
    http://msdn.microsoft.com/en-us/libr...ardoutput.aspx

  3. #3
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut
    Merci pour ton aide et pour le lien !

    J'ai donc fait ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
                    Process myProcess = new Process();
                    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("flv.exe");
                    myProcessStartInfo.Arguments = GetParameters();
                    myProcessStartInfo.UseShellExecute = false;
                    myProcessStartInfo.RedirectStandardOutput = true;
                    myProcess.StartInfo = myProcessStartInfo;
                    myProcess.Start();
     
                    StreamReader myStreamReader = myProcess.StandardOutput;
                    string myString = myStreamReader.ReadLine();
                    Console.WriteLine(myString);
                    myProcess.Close();
    Mais myString est null ! Je ne comprend vraiment pas ! Est ce que cela peut venir du fait que le programme lancé retourne les valeurs dans un certain format ??

  4. #4
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Et si tu commençais par recopier le code sur la page de la msdn et changer juste la valeur du l'exe à appeler ?

    Le code que tu me montres y ressemble pas vraiment.

  5. #5
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut
    Euh c'est justement ce que j'ai fait

    Ou alors on parle pas de la même chose, voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
                    Process myProcess = new Process();
                    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("flv.exe");
                    myProcessStartInfo.Arguments = GetParameters();
                    myProcessStartInfo.UseShellExecute = false;
                    myProcessStartInfo.RedirectStandardOutput = true;
                    myProcess.StartInfo = myProcessStartInfo;
                    myProcess.Start();
     
                    StreamReader myStreamReader = myProcess.StandardOutput;
                    string myString = myStreamReader.ReadLine();
                    Console.WriteLine(myString);
                    myProcess.Close();
    Et voici le code MSDN :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    Process myProcess = new Process();
    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("Process_StandardOutput_Sample.exe" );
    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo = myProcessStartInfo;
    myProcess.Start();
     
    StreamReader myStreamReader = myProcess.StandardOutput;
    // Read the standard output of the spawned process.
    string myString = myStreamReader.ReadLine();
    Console.WriteLine(myString);
    myProcess.Close();
    La seule différence se trouve ici :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    myProcessStartInfo.Arguments = GetParameters();
    Mais sans ces arguments mon exe ne marche pas évidemment.

  6. #6
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Ah je regardais un autre code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     // Start the child process.
     Process p = new Process();
     // Redirect the output stream of the child process.
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.FileName = "Write500Lines.exe";
     p.Start();
     // Do not wait for the child process to exit before
     // reading to the end of its redirected stream.
     // p.WaitForExit();
     // Read the output stream first and then wait.
     string output = p.StandardOutput.ReadToEnd();
     p.WaitForExit();

  7. #7
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut
    Ah ok je me dissais aussi ^^.

    Je test ça ce soir, et si ça marche pas je tente de mettre à la place de mon exe un fichier .bat avec une commande dir !

    Comment ça je saurais enfin si ça vient du code et de l'exe !

    Merci pour ton aide en tout cas.

    P.S : Laetitia te fais des poutoux.

  8. #8
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut
    Après essai le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
                    // Start the child process.
                    p = new Process();
                    // Redirect the output stream of the child process.
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.FileName = "flv.exe";
                    p.StartInfo.Arguments = GetParameters();
                    p.Start();
                    // Do not wait for the child process to exit before
                    // reading to the end of its redirected stream.
                    // p.WaitForExit();
                    // Read the output stream first and then wait.
                    string output = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();
    Ne fonctionne pas mieux, ma stirng output = "" !!!
    Alors que le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
                   // Start the child process.
                    p = new Process();
                    // Redirect the output stream of the child process.
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.Arguments = "dir"; GetParameters();
                    p.Start();
                    // Do not wait for the child process to exit before
                    // reading to the end of its redirected stream.
                    // p.WaitForExit();
                    // Read the output stream first and then wait.
                    string output = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();
    Me retourne bien des lignes !!! Il semblerait que le problème vienne de mon exe, c'est possible ça ?

  9. #9
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Tu es sûr que ton programme écrit sur la sortie standard ?

  10. #10
    Membre confirmé Avatar de Anto03
    Inscrit en
    Octobre 2005
    Messages
    155
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 155
    Par défaut
    En effet c'était bien ça, je me suis posé la même question que toi et dans le doute j'ai regardé la sortie "StandardError"...

    Et toutes les sorties sont redirigées ici ! Ce qui me parait étrange, mais cela doit dépendre de comment est écrit le programme ?

    Merci pour ton aide en tout cas !!

  11. #11
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Citation Envoyé par Anto03 Voir le message
    En effet c'était bien ça, je me suis posé la même question que toi et dans le doute j'ai regardé la sortie "StandardError"...

    Et toutes les sorties sont redirigées ici ! Ce qui me parait étrange, mais cela doit dépendre de comment est écrit le programme ?

    Merci pour ton aide en tout cas !!
    Wep ça dépend de comment le programme a été écrit, mais bon en général on utilise la sortie d'erreur pour écrire en cas d'erreur

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 25/11/2014, 12h57
  2. Process -- Récupérer les sorties d'un service
    Par Arnard dans le forum Débuter
    Réponses: 1
    Dernier message: 01/10/2009, 10h14
  3. Réponses: 6
    Dernier message: 12/04/2006, 15h53
  4. [JSP][Process] Récupérer message
    Par hedgehog dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 20/07/2005, 14h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo