473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Executing system calls

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
4 2482
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
7
by: Jibey | last post by:
Hello: I'm facing a very strange problem. When I run my Web application in Visual Studio.NET the Page_Load event is not executing. Other events like a Button_Click are executing. It doesn't...
2
by: Cédric Rossé | last post by:
Hello, I'm currently developping a web application and I'm trying to execute a batch file (.cmd) on the server side when the client presses a button on a web page. When I do this on my local...
4
by: sklett | last post by:
I'm fairly new to Threading and things have been going pretty smooth so far. I did some performance testing on my new app and realized it's lagging a bit on raising events. It's a realtime app and...
7
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
1
by: swatibksh | last post by:
i need to execute system calls of windows on remote machine in a LAN. how can i do so using java ?
1
by: swatibksh | last post by:
Hi I didn't exactly get how to execute system calls on "remote " machine on windows. For e.g. how can i run system file for getting the list of running processes on a...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
0
Frinavale
by: Frinavale | last post by:
I have a peculiar problem... Background: I have a function that I don't want the user to execute more than once while they are waiting for it to process; therefore, I disable all of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.