如何在C#中从Windows服务调用exe [英] How to call exe from windows service in C#

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

问题描述

我想从Windows 10的Windows服务中调用外部exe.但是,当我安装该服务时,它不起作用.要运行该服务,我需要更改该服务的登录属性.但是我不想在安装服务后进行任何更改.它应该按原样运行.

我尝试过的事情:

I wanted to call the external exe from windows service in windows 10. But when I install the service it does not work. To run the service I need to change the logon properties for that service. But I don''t want to make any changes after installation of service. It should run as it is.

What I have tried:

Tried various examples given on blogs but no luck.

推荐答案

根据您的描述,您尝试运行的可执行文件需要权限,但该权限未授予该帐户您正在用来运行服务.由于这是Windows安全系统的一部分,因此您无法更改行为.

例如,如果要运行的可执行文件需要访问网络路径,而您正在使用本地帐户运行服务,则启动的可执行文件将使用本地帐户,并且必然无法访问网络资源.

如果您不想更改服务帐户,请尝试使用其他明确指定的凭据启动可执行文件.您可以通过在 ProcessStartInfo类中定义帐户来定义要使用的帐户(System.Diagnostics) [
Based on your description the executable you''re trying to run, needs privileges that are not granted to the account you''re using to run the service. As this is part of Windows security system, you can''t change the behaviour.

For example if the executable to run needs access to a network path and you''re running the service using a local account, then the started executable uses the local account and necessarily cannot access the network resources.

If you don''t want to change the service account then try starting the executable using different, explicitly specified credentials. You can define the account to use by defining them in ProcessStartInfo Class (System.Diagnostics)[^]


Windows服务在会话0中运行,该会话与任何用户会话都是隔离的,因此没有任何屏幕与之相连.任何具有从服务启动的GUI的应用程序都只会挂在后台的某个地方,但永远不会对用户可见.
A windows service runs in session 0 which is isolated from any user session and thus has no screen attached to it. Any application with a GUI started from a service will just hang somewhere in the background but never become visible to a user.


Mike,感谢您提供这些宝贵的信息.我已经尝试过以下代码,但仍然没有运气.

Mike, thanks for this valuable information. I have tried below code but still no luck.

void OpenWithStartInfo()
{
    ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Minimized;

    Process.Start(startInfo);

    startInfo.Arguments = "www.northwindtraders.com";

    Process.Start(startInfo);
}


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

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