如何在C#中调用Windows服务中的exe [英] How to call exe in Windows Services in C#

查看:53
本文介绍了如何在C#中调用Windows服务中的exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

请建议我,我可以在Windows服务中拨打exe。



非常感谢。



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Diagnostics;
使用 System.Linq;
使用 System.ServiceProcess;
使用 System.Text;
使用 System.IO;
使用 System.Threading;

命名空间 testOCRSERvices
{
public partial class Service1:ServiceBase
{
public Service1()
{
InitializeComponent();
}


public void OnDebug()
{
// OCRProcess();
OnStart(< span class =code-keyword> null );
// callprocess();
}


受保护 覆盖 void OnStart( string [] args)
{
// callprocess();

// 主题t1 = new Thread(new ThreadStart(callprocess));
// t1.SetApartmentState(ApartmentState .STA);
// t1.Start();


// Console.WriteLine(& quot; TEstOCR Started& quot;) ;
BackgroundW orker bw = new BackgroundWorker();

bw.DoWork + = new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();



}
// private void bw_DoWork (对象发件人,DoWorkEventArgs e)
// {
// callprocess();
// }


private void bw_DoWork( object sender,DoWorkEventArgs e)
{
PDfSpliter.Impersonate a = new PDfSpliter.Impersonate();

if (a.impersonateValidUser(& quot; Administrator& quot;,& quot; abcd& quot;,& quot; rerere& quot;))
{

string cpath = @ C:\ Program Files \abc.exe;
string filename = Path.Combine(cpath,& quot; abc .exe& quot;);


流程p = 流程();

p.StartInfo.FileName = Path.Combine(cpath,& quot; abc.exe& quot;);





p.StartInfo.CreateNoWindow = true ;
p.StartInfo.ErrorDialog = false ;
p.StartInfo.RedirectStandardError = true ;
p.StartInfo.RedirectStandardInput = true ;
p.StartInfo.RedirectStandardOutput = true ;


p.StartInfo.UseShellExecute = false ;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;


p.Start();
p.WaitForExit();

base .Stop();


a.undoImpersonation();
}

}
public void callprocess( )
{

string cpath = @& quot; C:\Program Files \ abc.exe& quot;
string filename = Path.Combine(cpath,& quot; abc.exe& quot;);



流程p = new Process();

p.StartInfo.FileName = Path.Combine(cpath,& quot; abc.exe& quot;);




p.StartInfo.CreateNoWindow = true ;
p.StartInfo.ErrorDialog = false ;
p.StartInfo.RedirectStandardError = true ;
p.StartInfo.RedirectStandardInput = true ;
p.StartInfo.RedirectStandardOutput = true ;
p.StartInfo.UseShellExecute = false ;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;


p.Start();
p.WaitForExit();


}
受保护 覆盖 void OnStop()
{
// callprocess();
}
}
}

解决方案

这可能是只有你的一个问题。



您告诉Process Class重定向输入,输出和错误但是您没有提供代码来处理重定向。



参见将参数发送到CMD行或进程



尝试阅读您所属的每个属性的文档设置。

Hi There,
Please suggest me so i can call exe in windows services.

Many Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;

namespace testOCRSERvices
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }


        public void OnDebug()
        {
            //OCRProcess();
             OnStart(null);
            //callprocess();
        }


        protected override void OnStart(string[] args)
        {
            //callprocess();

            //Thread t1 = new Thread(new ThreadStart(callprocess));
            //t1.SetApartmentState(ApartmentState.STA);
            //t1.Start();


           // Console.WriteLine(&quot;TEstOCR Started&quot;);
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerAsync();



        }
        //private void bw_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    callprocess();
        //}


        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            PDfSpliter.Impersonate a = new PDfSpliter.Impersonate();

            if (a.impersonateValidUser(&quot;Administrator&quot;, &quot;abcd&quot;, &quot;rerere&quot;))
            {

                string cpath = @"C:\Program Files\abc.exe";
                string filename = Path.Combine(cpath, &quot;abc .exe&quot;);


                Process p = new Process();

                p.StartInfo.FileName = Path.Combine(cpath, &quot;abc.exe&quot;);





                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.ErrorDialog = false;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;


                p.StartInfo.UseShellExecute = false;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;


                p.Start();
                p.WaitForExit();

                base.Stop();


                a.undoImpersonation();
            }

        }
        public void callprocess()
        {

            string cpath = @&quot;C:\Program Files\abc.exe&quot;
            string filename = Path.Combine(cpath, &quot;abc.exe&quot;);



            Process p = new Process();

            p.StartInfo.FileName = Path.Combine(cpath, &quot;abc.exe&quot;);




            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.ErrorDialog = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;


            p.Start();
            p.WaitForExit();


        }
        protected override void OnStop()
        {
            //callprocess();
        }
    }
}

解决方案

This may be only one of your problems.

You're telling the Process Class to redirect input, output and errors but then you do not provide the code to handle the redirection.

See Solution 1 of Sending Argument to CMD line or process

Try reading the documentation for each of the properties that you are setting.


这篇关于如何在C#中调用Windows服务中的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆