|
|
#1 |
|
Lively Member
Join Date: Jul 05
Location: Belgium
Posts: 68
![]() |
I'm trying to run an exe (also providing a parameter) in my program, and saving the multiline output if it.
VB Code:
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. |
|
|
|
|
|
#2 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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, 2008, 2009, 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 | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#3 |
|
Lively Member
Join Date: Jul 05
Location: Belgium
Posts: 68
![]() |
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:
ps. Was that the use of string.format you meant?
Last edited by Guiseppe; Jul 18th, 2006 at 05:16 AM. |
|
|
|
|
|
#4 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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, 2008, 2009, 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 | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#5 |
|
Lively Member
Join Date: Jul 05
Location: Belgium
Posts: 68
![]() |
Re: [02/03] Getting output from shell
Hmm you're right.
I'll try this. Thanks a bunch. |
|
|
|
|
|
#6 |
|
Lively Member
Join Date: Jul 05
Location: Belgium
Posts: 68
![]() |
Re: [02/03] Getting output from shell
Hmm, for some reason this didn't work:
VB Code:
VB Code:
|
|
|
|
|
|
#7 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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:
__________________
![]() 2007, 2008, 2009, 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 | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET Last edited by jmcilhinney; Jul 18th, 2006 at 07:00 AM. |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|