0

I'm trying to run the following:

String command = @"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder;

System.Diagnostics.Process.Start("CMD.exe", "/K PATH C:\\Program Files\\R\\R-3.1.1\\bin;%path%"); 
System.Diagnostics.Process.Start("CMD.exe", "/K " + command);

Nothing happens when I execute it, does anyone know why? If I try

System.Diagnostics.Process.Start("CMD.exe", "/K MD TEST");

That works fine :s

e: Some extra info, The first command is setting the PATH so that the Rscript can be called by just typing Rscript. Also, both of these commands work when I do them in a normal CMD interface.

MatthewMartin
  • 31,164
  • 31
  • 106
  • 162
csdacac
  • 107
  • 1
  • 2
  • 11

3 Answers3

2

Prepare a batch file and execute it

using(StreamWriter sw = new StreamWriter("runscript.cmd", false))
{
   sw.WriteLine(@"PATH C:\Program Files\R\R-3.1.1\bin;%path%"); 
   sw.WriteLine(@"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder);
}
System.Diagnostics.Process.Start("CMD.exe", "/K runscript.cmd"); 

This assumes that you have read/write permissions on the current directory. You can change the location to a more suitable position using

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileCmd = Path.Combine(path, "runscript.cmd");
using(StreamWriter sw = new StreamWriter(fileCmd, false)
....
Steve
  • 208,592
  • 21
  • 221
  • 278
0

As realised by Steve, I was running two console processes. To resolve this I simply ran both commands in the same process.

csdacac
  • 107
  • 1
  • 2
  • 11
0

cmd.Arguments = "/K \"" + fullFilePath;

*try double " " right before PATH

aCo
  • 71
  • 4