gpt4 book ai didi

c# - 无法加载可执行文件

转载 作者:可可西里 更新时间:2023-11-01 14:36:06 24 4
gpt4 key购买 nike

我正在尝试创建一个可以打开应用程序的服务,但是我没有成功。因此,我下载了以下示例代码,并尝试基于它创建服务。但是,它也不起作用。发生的是代码执行,但从未调用可执行文件(在以下情况下,计算器未打开)。

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.Threading.Tasks;

namespace Open_Calculator
{
public partial class Service1 : ServiceBase
{
public static Process process;
public Service1()
{
//InitializeComponent();
string[] args = { "1", "2" };
OnStart(args);
}
protected override void OnStart(string[] args)
{
start_calc();
}
protected override void OnStop()
{
}
static protected void start_calc()
{
process = new Process();
process.StartInfo.FileName = @"C:\Windows\system32\calc.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
}
}
}

在控制台应用程序中复制粘贴的完全相同的代码可以工作。

该服务设置为与桌面交互,并设置为以“LocalSystem”运行。

在我测试时,运行该服务的帐户确实可以访问 C:\Windows\System32。 Process.Start() 没有返回任何错误,并且 calc 没有在后台运行(通过任务管理器检查)

最佳答案

以“以管理员身份运行”权限执行它

关于c# - 无法加载可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20004026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com