This question is an exact duplicate of:
这个问题完全是:
- Calling a batch file from a windows service 3 answers
- 从windows服务3中调用批处理文件。
I am having batch file : sample.bat with following code:
我有批处理文件:sample。蝙蝠与下面的代码:
@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:
还创建了另一个批处理文件:cmdSample。蝙蝠与下面的代码:
sample 2 4
示例2 4
So if i run cmdSample.bat file, it gives me correct result.
如果我运行cmdSample。bat文件,它给了我正确的结果。
After that i have created 1 windows service application in which i tried to call that batch file and pass the command, as follows:
在此之后,我创建了一个windows服务应用程序,我尝试调用该批处理文件并通过该命令,如下所示:
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??
谁能帮助我,为什么我不能通过windows服务应用程序来执行批处理文件??
Thanks in advance..
提前谢谢. .
1 个解决方案
#1
0
Your parameters are off, you are giving "sample" as your first parameter to the sample.bat.
您的参数已经关闭,您将“sample”作为您的第一个参数给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.
您需要使用cmd.exe启动您的batchfile。你可以在这里找到一个很好的解释。
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.
你想做什么?从windows服务运行批处理文件没有多大意义,您将无法看到结果。也许您应该先尝试一个控制台应用程序来调试您的问题。
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".
您可能还希望下次发布一个实际的错误,因为上面的所有内容只是猜测,我们需要更多的信息,而不仅仅是“不起作用”。
#1
0
Your parameters are off, you are giving "sample" as your first parameter to the sample.bat.
您的参数已经关闭,您将“sample”作为您的第一个参数给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.
您需要使用cmd.exe启动您的batchfile。你可以在这里找到一个很好的解释。
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.
你想做什么?从windows服务运行批处理文件没有多大意义,您将无法看到结果。也许您应该先尝试一个控制台应用程序来调试您的问题。
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".
您可能还希望下次发布一个实际的错误,因为上面的所有内容只是猜测,我们需要更多的信息,而不仅仅是“不起作用”。