Results 1 to 4 of 4

Thread: Execute Shell command in Mono

  1. #1
    Join Date
    Nov 2006
    Beans
    105
    Distro
    Ubuntu 7.04 Feisty Fawn

    Execute Shell command in Mono

    Hi all

    I want to execute this command in mono using c# and get the results to show in a combobox.
    How do I execute the command and get the results as a string or an array?
    Code:
     sudo fdisk -l | grep NTFS
    Last edited by hadiriazi; December 17th, 2006 at 10:31 AM.

  2. #2
    Join Date
    Nov 2006
    Beans
    105
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Execute Shell command in Mono

    Ok guys I now know how to execute the command, but how do I get the results?

    I used this code to execute the command:

    Code:
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.EnableRaisingEvents=false; 
    proc.StartInfo.FileName = "fdisk";
    proc.StartInfo.Arguments = "-l | grep NTFS";
    proc.Start();
    proc.WaitForExit();
    plz help me
    Last edited by hadiriazi; December 17th, 2006 at 12:13 PM.

  3. #3
    Join Date
    Jul 2006
    Location
    Trinidad and Tobago
    Beans
    160
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Execute Shell command in Mono

    I have no idea if you still need this info, but your post was the first that came up when I searched in google for how to call a command from c#.

    On this page there is some code that deals with reading the stdout & stderr:
    http://msdn2.microsoft.com/en-us/lib...ss(vs.80).aspx

    HTH,
    - rmjb
    Registered Linux User #422402

  4. #4
    Join Date
    Mar 2007
    Beans
    172

    Re: Execute Shell command in Mono

    He is correct. Each process has a stream of data going in and out essentially. System.Diagnostics.Process.StandardOutput is the stream of the output of the process.
    http://www.go-mono.com/docs/index.as...em.Diagnostics

    It's gets a System.IO.StreamReader. The member string ReadToEnd() will read all the data returned. It is a synchronous call so you do not have to handle waiting until it returns anything. It will block untill something is read.

    Here is an example:
    Code:
    string data = process.StandardOutput.ReadToEnd();
    
    Console.Writeline( data + " was returned" );

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •