Forum guidelines
AuthorQuestion
Cannot get result from Process.Start ...
Posted on: 12 Jul 2010
Dear friends,

Just arrived to C#Corner. I'm having a lot of problems getting a result that seems so obvious and easy.

Take a look at the following code:

Process p = new Process();

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = "openssl\\openssl.exe";

p.StartInfo.Arguments = "\"2010-05-18;2010-05-18T11:22:19;FAC 001/14;3.12; \" | openssl dgst -sha1 -sign prikeyofs.pem | openssl enc -base64";

p.Start();

string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();

This example returns an empty string. And why is this happening?

If I replace the file name for cmd and remove the arguments I get the usual "Microsoft Windows [Versão 6.1.7600]...". I can't even send the result to another file like "> something.txt".

Does anyone knows why is this happening.

Thanks in advance.

Best regards.

AuthorReply
Sam Hobbs
  • 0
  • 0
accepted
Re: Cannot get result from Process.Start ...
Posted on: 14 Jul 2010   Accepted Answer
You moved WaitForExit to after the ReadToEnd so it is expected that it would not work and work the way you say.

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Cannot get result from Process.Start ...
Posted on: 12 Jul 2010  
You need to state explicitly (here) that the other process is a console program becasue if it is not then there is more that you need to do. If it is a console program then you must ensure that the programs writes to standard output. You also must ensure the program has a chance to write the data out before you read the data. I am not sure about how redirection of standard output works using .Net, but it makes wsense to me that ReadToEnd() does not work. I don't know if some other method will wait until there is data to read but it might be that ReadToEnd() will not wait for data whereas another method might wait.
Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Cannot get result from Process.Start ...
Posted on: 13 Jul 2010  

Hi Sam,
Thank you for your answer. Yes, the other process is a console program. Isn't the program already writing to standard output? Probably ReadToEnd() is not working, but if I use, for example, the 'cmd' command, it returns me the string "Microsoft Windows [Versão 6.1.7600]..." ...
I'm kind of lost here ... :|
Thanks a lot!

Re: Cannot get result from Process.Start ...
Posted on: 13 Jul 2010  
Yes, the command processor (cmd.exe) will write to standard output. That has no relevance to whether the program you are executing writes to standard output.

What happens if you execute the program in a command prompt window and redirect standard output that way? That is, use ">" or "|" in the command line; do you get the output you need that way? If you do not then you nearly certainly cannot get the data using the source code you show in this thread.

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Cannot get result from Process.Start ...
Posted on: 13 Jul 2010  

Dear Sam,
Thank you once again for your response. I've managed so solve the problem taking a look at the p.StandardError output .. It was giving a 'File not found..' error ... how stupid could I be ... :(
However, I came up with another problem. I using that same openSSL.exe to cypher some string, and the get the result. The problem is that the result comes with special chars and I loose some chars when passing the result to a string ...
Any ideas? Do you think byte [] will solve my problem?
Thank you very much.

Re: Cannot get result from Process.Start ...
Posted on: 13 Jul 2010  
Good. It is good that you solved the first problem. I apologize for being pessimistic. It is often difficult to know about things such as this.

I am sorry that in my previous post I asked about redirection when you did say:

I can't even send the result to another file like "> something.txt".

As for losing characters, determine if the data is Unicode; that makes a difference. A console program it is probably not using Unicode but if it is then you need to also use Unicode. Otherwise the problem is not the data; the problem is what you are using to show the data. In other words, the font or character set. I don't know enough to be able to give a clear answer but I hope that helps you to look in the best direction. If you need more help, create a new thread here and give it a relevant subject so that someone that is most likely to be able to help will help you.


Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Cannot get result from Process.Start ...
Posted on: 14 Jul 2010  

Dear Sam,
No, I am the one that is sorry for bothering you.
I got it fixed! Somehow I was loosing characters, and I couldn't understand why ... and I still don't,lol.
Take a look on how I solved the problem:
******************
Process
pSign = new Process();
pSign.StartInfo.UseShellExecute = false;
pSign.StartInfo.RedirectStandardOutput = true;
pSign.StartInfo.RedirectStandardError = true;
pSign.StartInfo.StandardOutputEncoding = Encoding.ASCII;
pSign.StartInfo.FileName = "cmd.exe";
pSign.StartInfo.Arguments = "/c echo \"ola\" | openssl dgst -sha1 -sign prikeyofs.pem " + "| openssl enc -base64";
pSign.Start();
pSign.WaitForExit();
string X = pSign.StandardError.ReadToEnd();
string ola = pSign.StandardOutput.ReadToEnd();
*************************
The variable ola now has the wished result because it comes in a base64. Otherwise, I will loose the chars and the cypher is lost ..
Weird don't you think?
Once again, thank you for being there for me.
Best wishes!

Re: Cannot get result from Process.Start ...
Posted on: 14 Jul 2010  

Probably, but dispite of my problem is solved, I will remain confused because of the encoding problem. I just can't understand how could it be possible to loose all that characters.
Sam, thank you very much once again.

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.