By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Executing system calls

P: n/a
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());
}

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 Viltersten" wrote:
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());
}

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #2

P: n/a
MC
I believe

System.Diagnostics.Process.Start("cmd.exe","/c myfile.bat");

will fill the bill. Because the .bat file is not directly executable (like
an .exe), you have to start cmd.exe and use myfile.bat as its argument.
Further arguments can come after myfile.bat.

See:
http://msdn.microsoft.com/en-us/libr...52(VS.71).aspx

This is one of a number of things that are in System.Diagnostics that aren't
diagnostics, just plain OS interfacing.
Jul 23 '08 #3

P: n/a
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!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 24 '08 #4

P: n/a
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!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 25 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.