Solved

Commandline output to Windows Program

Posted on 2008-10-19
11
346 Views
Last Modified: 2013-12-17
Dear Experts,

I have a command line program which when run, outputs something like this from cmd:

version;3
active;1
interface;eth0
nick;eth0
created;1224363832
updated;1224407701
totalrx;622
totaltx;198
currx;24485590
curtx;1987530
...
and it goes on

How do I capture the value after "totalrx" and display it in a textbox?

Please help.
0
Comment
Question by:narmi2
  • 6
  • 4
11 Comments
 
LVL 3

Expert Comment

by:ddanonimity
ID: 22751539
try this

StreamReader lo_reader = new StreamReader(@#FILE#);
lo_reader.open();
while(!lo_reader.EndOfStream)
{
string ls_input = lo_reader.ReadLine();
      if(ls_input.Contains("totalrx")
      {
            ls_input = ls_input.Replace("totalrx;");
            textbox1.Text = ls_input
      }
}
lo_reader.close();

if you want to display the totalrx before it just remove the replace line. and remember to include the
#include System.IO;
at the top.

have done it quite quickly so you'll have to substitue your names in and beware of caps I might have missed.
0
 
LVL 1

Author Comment

by:narmi2
ID: 22751554
Is this code reading text from a file?
0
 
LVL 1

Author Comment

by:narmi2
ID: 22751575
so far I have this which captures everything and sticks it into a textbox.  I don't know how to display totalrx in the textbox alone?

      protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
      {
            Process p = new Process();
            
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "the commandline program to run goes here";
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            
            entry1.Text = output";
      }
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22752683
Hi narmi2;

You can use a Regular Expression to get the text for the textBox control as shown in the line below.

Fernando

using System.Text.RegularExpressions;
 

entry1.Text = Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value;

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22752745
Hi narmi2;

The meaning of the Regex pattern, "(?i)totalrx;(.*?)\r", is as follows:

  1. (?i) - Tells Regex that the pattern that follows should be evaluated with case insensitive matching.
  2. totalrx; - Look for this sequence of characters in the string.
  3. (.*?)  - Group the next sequence of characters until the first character after the end group, ).
  4. \r - This is a return character and tells to group to stop grouping, in this case.
In this command, Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value;, the Groups[1] tells Regex to return Group 1 the only group in this pattern and Value returns the string of the group.

Fernando
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:narmi2
ID: 22761880
For some reason the regex is not returning anything?

I tried this, but it's output was blank:

Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value);

If I do the following, it displays text like the text I posted in the original post:

Console.WriteLine (output);
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22762279
Please post your code. I have tested this solution before posting.
0
 
LVL 1

Author Comment

by:narmi2
ID: 22764715
Here's the code:
using System;

using System.Diagnostics;

using System.Text.RegularExpressions;
 

namespace test_console

{

	class MainClass

	{

		public static void Main(string[] args)

		{

                 ProcessStartInfo psi = new ProcessStartInfo ();

                 psi.FileName = "command to execute goes here";

                 psi.Arguments = "optional argument goes here";

                 psi.RedirectStandardOutput = true;

                 psi.UseShellExecute = false;

 

                 Process p =  Process.Start (psi);

                 string ret = p.StandardOutput.ReadToEnd ();
 

                 p.WaitForExit ();

                 Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value);

		}

	}

}

Open in new window

0
 
LVL 1

Author Comment

by:narmi2
ID: 22764721
Please ignore the copy/past error, the line:
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value);

Should have been:
Console.WriteLine (Regex.Match(ret, @"(?i)totalrx;(.*?)\r").Groups[1].Value);
0
 
LVL 1

Author Closing Comment

by:narmi2
ID: 31507544
Figured it out.  I am coding for Linux so instead of /r I hade to use /n
Thanks for all the help.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22767123
Yes you are correct files created on Unix type machine do use different line terminators.

Not a problem, glad I was able to help. ;=)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now