In my console application, I need to execute a series of commands:
D:
cd d:\datafeeds
grp -encrypt -myfile.xls
This set of commands actually encrypt a file using a tool (gpg).
How will I do it ?
In my console application, I need to execute a series of commands:
D:
cd d:\datafeeds
grp -encrypt -myfile.xls
This set of commands actually encrypt a file using a tool (gpg).
How will I do it ?
you could create a Process. To use this,exececute the generated .exe in the folder where grp is.
Process process1 = new Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.FileName = "cmd.exe";
process1.StartInfo.Arguments = "/C grp -encrypt -myfile.xls";
Other answers haven't mentioned the ability to set the WorkingDirectory. This eliminates the need for the directory changing operations and the need to store your executable in the datafeeds directory:
Process proc = new Process();
proc.StartInfo.WorkingDirectory = "D:\\datafeeds";
proc.StartInfo.FileName = "grp";
proc.StartInfo.Arguments = "-encrypt -myfile.xls";
proc.Start();
// Comment this out if you don't want to wait for the process to exit.
proc.WaitForExit();
Process.start lets you execute commands in the shell.
see this question: ShellExecute equivalent in .NET
Create a batch file that contains your commands.
Then use Process.Start and ProcessStartInfo class to execute your batch.
ProcessStartInfo psi = new ProcessStartInfo(@"d:\datafeeds\yourbatch.cmd");
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.WorkingDirectory = @"d:\datafeeds";
Process.Start(psi);
ProcessStartInfo contains other useful properties See MSDN docs
Process and ProcessStartInfo require an using System.Diagnostics;
In this scenario (when you need to run command line tools) I prefer to use the batch approach instead of coding everything through the ProcessStartInfo properties. It will be more flexible when you have to change something and you have not the code available.