2015-06-26 5 views
0

Я использую VB 2010. Моя проблема заключается в:Отправка и получение сообщений/из командной строки

Я могу послать и отобразить отправленную команду в текстовое поле без каких-либо проблем , но когда я пытаюсь использовать другую. ехе в ЦМД я не могу отобразить его в текстовом поле Мой исходный код:

Private Results As String 

    'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update. 
    Private Delegate Sub delUpdate() 
    Private Finished As New delUpdate(AddressOf UpdateText) 
    Private Sub UpdateText() 
     TextBox2.Text = Results 
    End Sub 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate) 
     CMDThread.Start() 
    End Sub 

    Private Sub CMDAutomate() 

     Dim myprocess As New Process 
     Dim StartInfo As New System.Diagnostics.ProcessStartInfo 

     'Starts the CMD Prompt 
     StartInfo.FileName = "cmd.exe" 
     StartInfo.RedirectStandardInput = True 
     StartInfo.RedirectStandardOutput = True 

     'Required to redirect 
     StartInfo.UseShellExecute = False 

     'Disables the creation of a CMD Prompt outside application. 
     StartInfo.CreateNoWindow = True 


     myprocess.StartInfo = StartInfo 
     myprocess.Start() 
     Dim SR As System.IO.StreamReader = myprocess.StandardOutput 
     Dim SW As System.IO.StreamWriter = myprocess.StandardInput 

     'Runs the command you entered... 
     SW.WriteLine(TextBox1.Text) 

     'Exits CMD Prompt 
     SW.WriteLine("exit") 

     'Displayes the results... 
     Results = SR.ReadToEnd 
     SW.Close() 
     SR.Close() 

     'Invokes Finished delegate, which updates textbox with the results text 
     Invoke(Finished) 
    End Sub 
End Class 

, например, когда я использую команду fastboot в ЦМД я получить

usage: fastboot [ <option> ] <command> 

commands: 
    update <filename>      reflash device from update.zip 
    flashall         flash boot + recovery + system 
    flash <partition> [ <filename> ]   write a file to a flash partition 
    erase <partition>      erase a flash partition 
    format <partition>      format a flash partition 
    getvar <variable>      display a bootloader variable 
    boot <kernel> [ <ramdisk> ]    download and boot kernel 
    flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it 
    devices         list all connected devices 
    continue         continue with autoboot 
    reboot         reboot device normally 
    reboot-bootloader      reboot device into bootloader 
    help          show this help message 

options: 
    -w          erase userdata and cache (and format 
              if supported by partition type) 
    -u          do not first erase partition before 
              formatting 
    -s <specific device>      specify device serial number 
              or path to device port 
    -l          with "devices", lists device paths 
    -p <product>        specify product name 
    -c <cmdline>        override kernel commandline 
    -i <vendor id>       specify a custom USB vendor id 
    -b <base_addr>       specify a custom kernel base address 
    -n <page size>       specify the nand page size. default: 
2048 
    -S <size>[K|M|G]       automatically sparse files greater th 
an 
              size. 0 to disable 

но когда я использую свою программу я получить

Microsoft Windows [version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation. Tous droits r‚serv‚s. 

C:\Users\[email protected]\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug>fastboot 

C:\Users\[email protected]\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug>exit 

ответ

0

Синтаксис выход для fastboot.exe когда создалась без аргументов отображается через поток ошибок; используйте RedirectStandardError, чтобы зафиксировать это;

StartInfo.RedirectStandardError = True 
Dim SE As System.IO.StreamReader = myprocess.StandardError 
+0

Une исключение де première шанс де типа «» System.InvalidOperationException s'est produite данс System.dll –

+0

Использование кода (хотя изменено немного, чтобы запустить в тестовом случае), это отлично работает для меня. Вы реализуете его так же, как и с StandardOutput? –

+0

'Отключает создание внешней подсказки CMD. StartInfo.CreateNoWindow = True StartInfo.RedirectStandardError = True myprocess.StartInfo = StartInfo myprocess.Start() дим SR В System.IO.StreamReader = myprocess.StandardOutput дим SW Как System.IO.StreamWriter = myprocess. StandardInput Dim SE As System.IO.StreamReader = myprocess.StandardError –

Смежные вопросы