Connecting Tech Pros Worldwide Help | Site Map

Executing system calls

K Viltersten
Guest
 
Posts: n/a
#1: Jul 23 '08
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.


=?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=
Guest
 
Posts: n/a
#2: Jul 23 '08

re: Executing system calls


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:
Quote:
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.
>
>
>
MC
Guest
 
Posts: n/a
#3: Jul 23 '08

re: Executing system calls


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.


K Viltersten
Guest
 
Posts: n/a
#4: Jul 24 '08

re: Executing system calls


Actually, that monstrocity as you called it won't work. For batch files,
Quote:
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.


K Viltersten
Guest
 
Posts: n/a
#5: Jul 25 '08

re: Executing system calls


Actually, that monstrocity as you called it won't work. For batch files,
Quote:
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.


Closed Thread