Executing system calls

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I want to execute a BAT-file as follows.
string dir = "c:\\temp\\";
string file = "batch.bat";
TheYetUnknownMethod(dir + file);

In Perl, for instance, i can call system()
with that string. Does something similar
exist for C#?

The only thing i've found is this huge
over-kill for monstrocity.

Process p = null;
try
{
string targetDir;
targetDir =
string.Format(@"C:\temp\");
p = new Process();
p.StartInfo.WorkingDirectory = targetDir;
p.StartInfo.FileName = "batch.bat";
p.StartInfo.Arguments =
string.Format("C-Sharp Console App");
p.StartInfo.CreateNoWindow = false;
p.Start();
p.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(
"Exception Occurred :{0},{1}",
ex.Message,ex.StackTrace.ToString());
}
 
Actually, that monstrocity as you called it won't work. For batch files, the
filename parameter should be cmd.exe. The first part of the arguments
property should be the batch file name.
 
Actually, that monstrocity as you called it won't work. For batch files,
the
filename parameter should be cmd.exe. The first part of the arguments
property should be the batch file name.


I'm fairly sure that, due to mu ignorance and what
not, i didn't mention "cmd.exe" BUT still got the
effect programmed in the batch file.

I might have tested RunAsShell-property (or
something similar). In any case, i'll correct the
error and retry, thanks!
 
Actually, that monstrocity as you called it won't work. For batch files,
the
filename parameter should be cmd.exe. The first part of the arguments
property should be the batch file name.

I beg to differ. In fact, as i noticed, the UseShellCommand is by
default set to true so my system actually CAN "execute" script.bat
even though it's a bit of cheating. :)

Also, it seems that the string i'm executing, i.e.
cmd.exe c:\temp\script.bat qwert asdfg
with
FileName = "cmd.exe";
Arguments = "c:\temp\script.bat qwert asdfg";
doesn't do what it's supposed to (even when called manually using
WIN+r keys... Any comments?

Thanks!
 
Back
Top