0

I am having batch file : sample.bat with following code:

@ECHO OFF

SET /a INT1=%1
SET /a INT2=%2

SET /a ANSWER=INT1*INT2

ECHO %ANSWER%

PAUSE

Also created another batch file : cmdSample.bat with following code:

sample 2 4

So if i run cmdSample.bat file, it gives me correct result.

After that i have created 1 windows service application in which i tried to call that batch file and pass the command, as follows:

private void DoWork()
    {
        try
        {
            string fname = @"C:\Users\of4\Desktop\sample.bat";
            string cmd = "sample 2 4";
            RunSampleBatch(fname, cmd);
        }
    }

    private void RunSampleBatch(string fname, string cmd)
    {
        Process p = new Process();
        p.StartInfo.FileName = fname;
        p.StartInfo.Arguments = cmd;
        p.Start();
    }

Can anyone help me, why i am not able to execute batch file through windows service application??

Thanks in advance..

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Alpa
  • 117
  • 2
  • 9

1 Answers1

0

Your parameters are off, you are giving "sample" as your first parameter to the sample.bat.

Your service is probably not running under your user account. Maybe it cannot even access your file.

You need to start your batchfile using cmd.exe. You can find a very good explanation here.

What are you trying to do? Running a batch file from a windows service doesn't make much sense, you won't be able to see the results. Maybe you should try a console application first to debug your problems.

You might also want to post an actual error next time, because all of the above is just guesswork, we need more info than just "doesn't work".

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks for your reply There was my mistake.Revised code is: And i have to put, cmdSample.bat file in application's bin\debug folder. Then it's works fine. But when i try to install windows service using setup project, it doesn't work. it takes default path as: C:\Wondows\System32 – Alpa Apr 04 '13 at 07:12
  • Revised Code is: private void ScheduledProcess() { try { string cmd = "cmdSample.bat"; RunSampleBatch(cmd); } } private void RunSampleBatch(string cmd) { ProcessStartInfo ProcessInfo = new ProcessStartInfo(); ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + cmd); Process.Start(ProcessInfo); } – Alpa Apr 04 '13 at 08:36
  • "doesn't work" is not a valid error description. You need to ask a good question for people to help you. – nvoigt Apr 04 '13 at 09:46