Comment faire pour lire le texte en sortie de mon shell ??
patfac1
Messages postés20Date d'inscriptionlundi 17 décembre 2001StatutMembreDernière intervention27 janvier 2006
-
27 janv. 2006 à 09:40
cs_eldim
Messages postés956Date d'inscriptionlundi 30 mai 2005StatutMembreDernière intervention21 août 2014
-
27 janv. 2006 à 10:47
Bonjour,
J'ai écris quelques lignes ( en vb.net ) pour lancer un shell depuis mon appli et savoir quand elle se termine.
Maintenant, il faudrait que je puisse récuperer le texte en sortie de mon shell; en l'occurence le résultat de mon upload FTP.
J'ai vu des exemple ou l'on récupère le contenu de la propriété StandardOutput avec au préalable StartInfo.RedirectStandardOutput placé à true mais ca ne marche pas , une exception est levée des que je veux utiliser StandardOutput .
J'ai aussi essayer de rajouter "> "c:\test.txt" à la fin de ma commande shell, mais ca ne marche pas non plus (???...) .Si qq1 a une idée , ca serait coool :)
Je laisse mon code pour que ca soit plus clair :)
Merci bcps à ceux qui prendrons le temps de m'aider :)
Patrick
Public Class test
Friend WithEvents objProcess As New Process
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Public Sub OnExited(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("Traitement terminé " & vbCrLf & objProcess.ExitCode)
Dim bibi As IO.StreamReader
bibi = objProcess.StandardOutput ---> C'est là que ca saigne ...
End Sub
Public Function fun_FtpPUT(ByVal sUser As String, ByVal sPass As String, ByVal sIP As String, ByVal sFile As String, ByVal sDest As String)
Fun_EcrisBatch(sUser, sPass, sFile, sDest)
' ici on récupère le process Id de notre shell et on l'utilise pour récup le process dans notre objet
objProcess = Process.GetProcessById(Shell("ftp -s:""c:\ftpbatch"" " & sIP, AppWinStyle.NormalNoFocus))
objProcess.EnableRaisingEvents = True
cs_eldim
Messages postés956Date d'inscriptionlundi 30 mai 2005StatutMembreDernière intervention21 août 20141 27 janv. 2006 à 10:47
Si tu veux j'ai une solution pour toi : c'est l'adaptation en VB.NET de "MyDOS" un source VB6 que j'ai du trouvé sur ce site. Je vai te donner la Class qui sert à gérer ça il te suffira de renseigner :
CommandLine
et d'exécuter :
ExecuteCommand
Voici la class :
Public
Class clsDOS
'API CreatePipe permet de créer un "pipe" anonyme,
'on récupére un handle pour lire et un pour ecrire.
Private
Declare
Function CreatePipe
Lib "kernel32" (
ByRef phReadPipe
As
Integer, _
ByRef phWritePipe
As
Integer,
ByRef lpPipeAttributes
As SECURITY_ATTRIBUTES, _
ByVal nSize
As
Integer)
As
Integer
'Utiliser pour lire le "pipe" rempli par le process qui
'sera créé par l'API CretaProcessA
Private
Declare
Function ReadFile
Lib "kernel32" (
ByVal hFile
As
Integer, _
ByVal lpBuffer
As
String,
ByVal nNumberOfBytesToRead
As
Integer, _
ByRef lpNumberOfBytesRead
As
Integer,
ByVal lpOverlapped
As
Integer)
As
Integer
'Structure utilisée par l'API CreateProcessA
Private
Structure SECURITY_ATTRIBUTES
Dim nLength
As
Integer
Dim lpSecurityDescriptor
As
Integer
Dim bInheritHandle
As
Integer
End
Structure
'Structure utilisée par l'API CreateProcessA
Private
Structure STARTUPINFO
Dim cb
As
Integer
Dim lpReserved
As
Integer
Dim lpDesktop
As
Integer
Dim lpTitle
As
Integer
Dim dwX
As
Integer
Dim dwY
As
Integer
Dim dwXSize
As
Integer
Dim dwYSize
As
Integer
Dim dwXCountChars
As
Integer
Dim dwYCountChars
As
Integer
Dim dwFillAttribute
As
Integer
Dim dwFlags
As
Integer
Dim wShowWindow
As
Short
Dim cbReserved2
As
Short
Dim lpReserved2
As
Integer
Dim hStdInput
As
Integer
Dim hStdOutput
As
Integer
Dim hStdError
As
Integer
End
Structure
'Structure utilisée par l'API CreateProcessA
Private
Structure PROCESS_INFORMATION
Dim hProcess
As
Integer
Dim hThread
As
Integer
Dim dwProcessID
As
Integer
Dim dwThreadID
As
Integer
End
Structure
'Cette API lance un commande et renvoie les infos sur le process
'dans la structure PRECESS_INFORMATION
Private
Declare
Function CreateProcessA
Lib "kernel32" _
(
ByVal lpApplicationName
As
Integer,
ByVal lpCommandLine
As
String, _
ByRef lpProcessAttributes
As SECURITY_ATTRIBUTES, _
ByRef lpThreadAttributes
As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles
As
Integer,
ByVal dwCreationFlags
As
Integer, _
ByVal lpEnvironment
As
Integer,
ByVal lpCurrentDirectory
As
Integer, _
ByRef lpStartupInfo
As STARTUPINFO,
ByRef lpProcessInformation
As PROCESS_INFORMATION) _
As
Integer
'Cette API Permet de terminer prématurément un process KILLLL :)
Private
Declare
Function TerminateProcess
Lib "kernel32" (
ByVal hProcess
As
Integer, _
ByVal uExitCode
As
Integer)
As
Integer
'Fermeture d'un handle
Private
Declare
Function CloseHandle
Lib "kernel32" (
ByVal hHandle
As
Integer)
As
Integer
'Constantes utilisée pour les API
Private
Const NORMAL_PRIORITY_CLASS
As
Integer = &H20
Private
Const STARTF_USESTDHANDLES
As
Integer = &H100
Private
Const STARTF_USESHOWWINDOW
As
Short = &H1S
Private mCommand
As
String
'Variable privée contenant la ligne de commande
Private mOutputs
As
String
'Variable privée pour la lecture du texte renvoié
Private ProcI
As PROCESS_INFORMATION
'Process utilisé
Private HLecturePipe
As
Integer
'Handle de lecture du "pipe"
Private HEcriturePipe
As
Integer
'Handle d'écriture du "pipe"
'Evénement de reception de donnée de l'objet
Public
Event ReceiveOutputs(
ByRef CommandOutputs
As
String)