0

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 ?

Akshay J
  • 5,362
  • 13
  • 68
  • 105

4 Answers4

1

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";
Alex Peta
  • 1,407
  • 1
  • 15
  • 26
  • I want to execute it by making "d:\datafeeds" as the current folder, since gpg will look for files in the current folder only. – Akshay J Nov 06 '12 at 21:09
1

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();
itsme86
  • 19,266
  • 4
  • 41
  • 57
0

Process.start lets you execute commands in the shell.

see this question: ShellExecute equivalent in .NET

Community
  • 1
  • 1
Pow-Ian
  • 3,607
  • 1
  • 22
  • 31
  • yes but for me its not a single command like copy etc. its a 3 step process. – Akshay J Nov 06 '12 at 21:07
  • then you run a couple of processes. the results will be in the directory. or check this question:http://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net – Pow-Ian Nov 06 '12 at 21:08
0

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.

Steve
  • 213,761
  • 22
  • 232
  • 286