我试图执行一个批处理文件,它自己运行。我现在试图通过将其部署为一个Windows服务来监听文件夹并使用文件监视器事件调用批处理文件,从而实现自动化。这里是代码 -C#批处理文件执行从不退出
void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
ServiceEventLog.WriteEntry(TheServiceName + " Inside fileSystemWatcher_Created() - ");
if (e.Name.Trim().ToUpper().Contains("FU4DGF_TRADES"))
{
try
{
Utilities.SendEmail("IAMLDNSMTP", 25, "[email protected]", "[email protected]", "[email protected]", "[email protected]", "StatPro BatchFile Execution Started ", "");
int exitCode;
// ProcessStartInfo processInfo;
ServiceEventLog.WriteEntry(TheServiceName + " Before creation of instance of Batch process - ");
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files (x86)\StatPro Suite\MonthlyUpload.bat";
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\StatPro Suite";
process.StartInfo.UseShellExecute = false;
ServiceEventLog.WriteEntry(TheServiceName + " Before start of Batch process - ");
process.Start();
ServiceEventLog.WriteEntry(TheServiceName + " After start of Batch process - ");
process.WaitForExit();
//while (!process.HasExited)
//{
// System.Threading.Thread.Sleep(100);
//}
ServiceEventLog.WriteEntry(TheServiceName + " After process.close - ");
System.Environment.ExitCode = process.ExitCode;
}
我可以从我的事件日志中看到它可以记录日志 - 开始批处理之前。大概在这之后,该过程通过调用process.Start()开始,但没有任何反应。事件日志中没有任何内容,服务仍在运行,即不会崩溃。没有错误。我可以从任务管理器看到,它确实调用了它应该通过批处理文件调用的exe文件,但是exe文件只是保留在内存不变的情况下,并且CPU使用率为0,表明这个exe文件没有做任何事情。如果我手动运行该批处理文件,它工作正常。任何想法可能会出错?
手动运行bat文件时....是否显示任何用户提示? – Viru
[在C#中执行批处理文件]的可能重复(http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – MethodMan
可能存在管理权限问题。请检查该 – Balaji