简体   繁体   English

如何在C#Windows Service项目中启动.bat文件

[英]How to launch a .bat file in a C# windows Service project

I create a C# Windows service project, then I want to run a .bat file, but I find that it does not run 我创建一个C#Windows服务项目,然后我想运行一个.bat文件,但发现它没有运行

        Process pInfo = new Process();
        pInfo.StartInfo.UseShellExecute=false;
        pInfo.StartInfo.CreateNoWindow=true;
        pInfo.StartInfo.FileName =bat file name ;

        pInfo.StartInfo.RedirectStandardOutput = true;

        pInfo.Start();

Could anyone help me? 有人可以帮我吗?

You can execute cmd.exe with "/C batchfile" as argument. 您可以使用“ / C batchfile”作为参数执行cmd.exe。 I don't remember if full path to cmd.exe is required, but I'm using it in my code: 我不记得是否需要cmd.exe的完整路径,但是我在代码中使用了它:

Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.CreateNoWindow = true;
// p.StartInfo.WorkingDirectory =   // I usually set this to the bat file's directory
p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
p.StartInfo.Arguments = string.Format("/C \"{0}\"", batchFilename);
p.StartInfo.ErrorDialog = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

This code is old so things might have been changed in recent versions of Windows/.NET framework, but it works. 该代码很旧,因此在Windows / .NET框架的最新版本中可能已进行了更改,但是它可以工作。

This is what we use to execute files from the command line: 这是我们用来从命令行执行文件的方法:

        Process         proc                = new Process();
        StringBuilder   sb                  = new StringBuilder();
        string[]        aTarget             = target.Split(PATH_SEPERATOR); 
        string          errorMessage;
        string          outputMessage;

        foreach (string parm in parameters)
        {
            sb.Append(parm + " ");
        }

        proc.StartInfo.FileName                 = target;
        proc.StartInfo.RedirectStandardError    = true;
        proc.StartInfo.RedirectStandardOutput   = true;
        proc.StartInfo.UseShellExecute          = false;
        proc.StartInfo.Arguments                = sb.ToString();

        proc.Start();

        proc.WaitForExit
            (
                (timeout <= 0)
                ? int.MaxValue : timeout * 
  NO_MILLISECONDS_IN_A_SECOND * NO_SECONDS_IN_A_MINUTE
            );


        errorMessage    = proc.StandardError.ReadToEnd();
        proc.WaitForExit();

        outputMessage   = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();

One of the things to check is to make sure that the application trying to execute the bat file has permissions to do so. 要检查的一件事是确保尝试执行bat文件的应用程序具有执行此操作的权限。 It's an easy thing to overlook. 这是一件容易忽略的事情。

Should it not be pInfo.UseShellExecute = true; 应该不是pInfo.UseShellExecute = true; for a batch file? 批处理文件?

This is a rights issue, service need rights to run batch file you can fix rights issues as follow 这是一个权限问题,服务需要权限才能运行批处理文件,您可以按照以下方式解决权限问题

  1. Install you service 安装服务
  2. In windows services find your newly created service 在Windows服务中找到您新创建的服务
  3. Right click on service, click on properties 右键单击服务,单击属性
  4. Under 'logOn' tab select radio button 'Local system account' and also select 'allow service to interact with desktop' 在“登录”标签下,选择“本地系统帐户”单选按钮,然后选择“允许服务与桌面交互”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM