2016-02-08 41 views
2

我试图执行一个批处理文件,它自己运行。我现在试图通过将其部署为一个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文件没有做任何事情。如果我手动运行该批处理文件,它工作正常。任何想法可能会出错?

+1

手动运行bat文件时....是否显示任何用户提示? – Viru

+1

[在C#中执行批处理文件]的可能重复(http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – MethodMan

+0

可能存在管理权限问题。请检查该 – Balaji

回答

0

您禁用UseShellExecute。这意味着你不能使用shell来执行文件bat文件不是可执行文件,它们是外壳脚本。

因为你不是重定向标准I/O,只需启用UseShellExecute,你应该没问题。

+0

没有用户提示。我确定没有。我也会尝试禁用UseShellExecute,看看是否有帮助。会更新你。 –

+0

@PrasadMatkar你已经禁用它 - 你需要*启用*它:) – Luaan