Executing system calls

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());
}
 
F

Family Tree Mike

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.
 
K

K Viltersten

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!
 
K

K Viltersten

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top