To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Jul 18th, 2006, 02:30 AM   #1
Guiseppe
Lively Member
 
Join Date: Jul 05
Location: Belgium
Posts: 68
Guiseppe is on a distinguished road (10+)
Resolved [RESOLVED] [02/03] Getting output from shell

I'm trying to run an exe (also providing a parameter) in my program, and saving the multiline output if it.

VB Code:
  1. Dim commandString As String = "\\" & Environment.MachineName & "\HOME\" & Environment.UserName & "\SCM\" & Environment.GetEnvironmentVariable("repository_name") & "\scm.exe views"
  2. Shell(commandString)

I tried adding " >> C:\test.txt" to redirect the output to a file, but that never happens. I do prefer a way to get the data directly instead of writing to a file tho.

Help would be appreciated.

ps. The string is correct, tried it manually.
Guiseppe is offline   Reply With Quote
Old Jul 18th, 2006, 02:55 AM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,542
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [02/03] Getting output from shell

Don't use Shell in .NET applications. Use Process.Start. You pass the path of the file to be executed to the first argument and any commandline parameters to the second argument. Process.Start is more flexible than Shell and has more functionality. You should read the help topics for the Process.Start method and the ProcessStartInfo class.

Also, I would suggest using the String.Format method rather than multiple concatenations. It is eminently more readable.
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Old Jul 18th, 2006, 04:12 AM   #3
Guiseppe
Lively Member
 
Join Date: Jul 05
Location: Belgium
Posts: 68
Guiseppe is on a distinguished road (10+)
Re: [02/03] Getting output from shell

I still can't find a way to access the output.
Is it wrong to assume I can use the shell command " >> C:\test.txt" ?

Looks to me I should even be able to access the output without this command, using the processinfo class, but I can't find any.
Found the RedirectStandardOutput property, which could be used to output to a file according to msdn. But how can I specify the file/outputstream to be used?

VB Code:
  1. Dim strCommand As String = String.Format("\\{0}\HOME\{1}\SCM\{2}\{3}", Environment.MachineName, Environment.UserName, repository_name, "scm.exe")
  2.             Dim strArguments As String = "views  >> C:\test.txt"
  3.             Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
  4.             startInfo.CreateNoWindow = True
  5.             startInfo.UseShellExecute = False
  6.             startInfo.RedirectStandardOutput = True
  7.             Process.Start(startInfo)

ps. Was that the use of string.format you meant?

Last edited by Guiseppe; Jul 18th, 2006 at 04:16 AM.
Guiseppe is offline   Reply With Quote
Old Jul 18th, 2006, 04:30 AM   #4
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,542
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [02/03] Getting output from shell

The MSDN topic for the RedirectStandardOutput has a code example for reading the redirected output. You need to keep a reference to the Process object returned by Process.Start. You can then access its StandardOutput property, which is type StreamReader. This is the same type you use to read from a text file. You can read from the Process's StandardOutput and then write the data to a file using a StreamWriter. You could call the WaitForExit method of the Process to block until it completes or else you could declare a handler for its Exited event and then read the output there.

You may be able to use the >> operator something like you're trying to but I have no specific knowledge as I've never used it, either inside or outside a .NET app.
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Old Jul 18th, 2006, 04:37 AM   #5
Guiseppe
Lively Member
 
Join Date: Jul 05
Location: Belgium
Posts: 68
Guiseppe is on a distinguished road (10+)
Re: [02/03] Getting output from shell

Hmm you're right.
I'll try this.

Thanks a bunch.
Guiseppe is offline   Reply With Quote
Old Jul 18th, 2006, 05:49 AM   #6
Guiseppe
Lively Member
 
Join Date: Jul 05
Location: Belgium
Posts: 68
Guiseppe is on a distinguished road (10+)
Re: [02/03] Getting output from shell

Hmm, for some reason this didn't work:

VB Code:
  1. Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
  2.             Dim proc As New Process
  3.             startInfo.UseShellExecute = False
  4.             startInfo.RedirectStandardOutput = True
  5.             [b]proc.Start(startInfo)[/b]
  6.             Dim reader As StreamReader = proc.StandardOutput
But this did:

VB Code:
  1. Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
  2.             Dim proc As New Process
  3.             startInfo.UseShellExecute = False
  4.             startInfo.RedirectStandardOutput = True
  5.             [b]proc.StartInfo = startInfo
  6.             proc.Start()[/b]
  7.             Dim reader As StreamReader = proc.StandardOutput
Guiseppe is offline   Reply With Quote
Old Jul 18th, 2006, 05:53 AM   #7
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,542
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [RESOLVED] [02/03] Getting output from shell

That's because you've messed up the first lot of code. The overload of Process.Start that you've used in the first snippet is a Shared member that returns a Process object. In your code 'proc' refers to a different Process object to the one that is actually executing. It should have been:
VB Code:
  1. Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
  2.            
  3.             startInfo.UseShellExecute = False
  4.             startInfo.RedirectStandardOutput = True
  5.             Dim proc As Process = Process.Start(startInfo)
  6.             Dim reader As StreamReader = proc.StandardOutput
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*

Last edited by jmcilhinney; Jul 18th, 2006 at 06:00 AM.
jmcilhinney is online now   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:16 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.