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:
|
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.
Sam HobbsPosted Jul 14, 2010, 11:55 AM
Tiago PinhoPosted Jul 14, 2010, 1:43 PM
Sam, thank you very much once again.
Tiago PinhoPosted Jul 14, 2010, 5:36 AM
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!
Sam HobbsPosted Jul 13, 2010, 4:20 PM
I am sorry that in my previous post I asked about redirection when you did say:
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.
Tiago PinhoPosted Jul 13, 2010, 10:36 AM
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.
Sam HobbsPosted Jul 13, 2010, 6:10 AM
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.
Tiago PinhoPosted Jul 13, 2010, 4:51 AM
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!
Sam HobbsPosted Jul 12, 2010, 4:16 PM