Click here to Skip to main content
15,889,648 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I was trying to make a program that runs an application (PGP) on the command prompt automatically. I have tried running the command manually on the command prompt and everything went smoothly. However, when I tried it on code, though there were no errors, the results were not the same compared to when I manually typed the command on the command prompt. The commands though were the same. Below is my code:

C#
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = false;
cmdText = @"/C pgp +batchmode C:\input.pgp\ -o C:\output.txt\";
startInfo.Arguments = cmdText;
process.StartInfo = startInfo;
process.Start();


The command that is working when I type on the command prompt is the following:

pgp +batchmode C:\input.pgp\ -o C:\output.txt\


Did you had any experience similar with this before? Are there additional switches for cmd.exe I need to add on the Arguments property to make it work like when I was typing it on the command prompt?
Posted
Updated 29-May-13 0:09am
v3
Comments
Richard MacCutchan 29-May-13 4:35am    
What was the difference between the two executions?
walterhevedeich 29-May-13 4:47am    
It was supposed to remove the prompt for a passphrase. It worked fine when the command was typed manually and it did what it was supposed to do. On the code, the prompt was also removed, but there was no output file generated. No errors on the console though.
Richard MacCutchan 29-May-13 5:19am    
What was the actual filename you tried to create? The text of your command parameters above does not look right to me.
walterhevedeich 29-May-13 6:05am    
The [input file] [output file] were actually changed for the sake of this post but these are just paths and filenames. Basically, PGP is supposed to decrypt the input file to the output file given. I'll update my question.
Richard MacCutchan 29-May-13 6:13am    
You have not provided the full path for pgp; are you sure that cmd can find it?

1 solution

Skip that "CMD.EXE".

This is a common mistake of the beginners who don't understand that CMD.EXE is a "usual" program, no better then yours. You program should be the parent process of your application, but instead, you play the role of the parent process of CMD.EXE, and you want CMD.EXE to play the role of the parent process for some other child process. This is possible, but totally redundant. I understand where it comes from: this is the result of user thinking, non-developer thinking.

All you need is this:
C#
System.Diagnostics.Process.Start("pgp", @"batchmode C:\input.pgp\ -o C:\output.txt\");
// but I think trailing '\' should be removed, so it should be:
System.Diagnostics.Process.Start("pgp", @"batchmode C:\input.pgp -o C:\output.txt");


—SA
 
Share this answer
 
Comments
walterhevedeich 29-May-13 21:34pm    
Great. It finally worked. Thanks for the help.
Sergey Alexandrovich Kryukov 29-May-13 23:38pm    
I knew that :-) You are very welcome.
Good luck, call again.
—SA
V. 30-May-13 3:15am    
pssst, it´s "knew" that ;-)
Sergey Alexandrovich Kryukov 30-May-13 10:39am    
Thank you very much. :-) I tend to do funny typo. In one of my first articles, I wrote "shell" instead of "shall" in the expression "Shall we... ?", and they printed it as is in the conference "Proceedings".
The guy who brought it to my attention was amazed and told me he though it was a pun; in fact, it was just a stupid typo... :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900