[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:
Dim commandString As String = "\\" & Environment.MachineName & "\HOME\" & Environment.UserName & "\SCM\" & Environment.GetEnvironmentVariable("repository_name") & "\scm.exe views"
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.
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.
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:
Dim strCommand As String = String.Format("\\{0}\HOME\{1}\SCM\{2}\{3}", Environment.MachineName, Environment.UserName, repository_name, "scm.exe")
Dim strArguments As String = "views >> C:\test.txt"
Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
Process.Start(startInfo)
ps. Was that the use of string.format you meant? :)
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.
Re: [02/03] Getting output from shell
Hmm you're right.
I'll try this.
Thanks a bunch.
Re: [02/03] Getting output from shell
Hmm, for some reason this didn't work:
VB Code:
Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
Dim proc As New Process
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
[b]proc.Start(startInfo)[/b]
Dim reader As StreamReader = proc.StandardOutput
But this did:
VB Code:
Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
Dim proc As New Process
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
[b]proc.StartInfo = startInfo
proc.Start()[/b]
Dim reader As StreamReader = proc.StandardOutput
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:
Dim startInfo As New ProcessStartInfo(strCommand, strArguments)
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
Dim proc As Process = Process.Start(startInfo)
Dim reader As StreamReader = proc.StandardOutput