【问题标题】:Cannot open Servicename service on computer '.'. Error on Starting Windows Service programatically using c#无法在计算机“.”上打开服务名称服务。使用 c# 以编程方式启动 Windows 服务时出错
【发布时间】:2014-02-27 13:44:25
【问题描述】:

我已经创建了一个 Windows 服务并将其安装在我的系统上。现在,根据我的要求,我必须使用 Windows 窗体应用程序中的按钮单击来启动和停止此 Windows 服务。 这是我的代码..

public partial class Form1 : Form
{
    string svcStatus;
    ServiceController myService;

    public Form1()
    {
        InitializeComponent();

        myService = new ServiceController();
        myService.ServiceName = "ServiceName";
        svcStatus = myService.Status.ToString();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        if (svcStatus == "Stopped")
        {
            myService.Start();   // START the service if it is already Stopped

            string svcStatusWas = "";  
            while (svcStatus != "Running")
            {
                if (svcStatus != svcStatusWas)
                {
                    Console.WriteLine("Status: " + svcStatus);
                }

                svcStatusWas = svcStatus;

                myService.Refresh();
                svcStatus = myService.Status.ToString();
            }
            Console.WriteLine("Service Started!!");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (svcStatus == "Running")
        {                
            myService.Stop();   // STOP the service if it is already Running

            string svcStatusWas = "";   
            while (svcStatus != "Stopped")
            {

                svcStatusWas = svcStatus;

                myService.Refresh();    
                svcStatus = myService.Status.ToString();
            }
            Console.WriteLine("Service Stopped!!");
        }
    }     
}

}

我收到错误“无法在计算机 '.' 上打开 Servicename 服务。”在这一行 myService.Start();

请帮帮我。

【问题讨论】:

  • 你尝试启动的服务真的叫“ServiceName”吗?
  • 机器上是否存在服务?您确定您使用的是服务名称,而不是服务可执行文件的文件名吗?
  • 您是否有权在您运行的用户下启动/停止服务?

标签: c# winforms windows-services


【解决方案1】:

如果您在 XP 机器上或在 Visual Studios 的调试器下运行,这可能不是问题,但只有在 Windows 7 机器上部署程序时,您才会看到错误:

无法在计算机“.”上打开 服务。

这很可能是因为自 Vista 以来发生的有关用户帐户控制的权限更改。

如果您尝试以管理员身份运行应用程序并且问题没有发生,您可以验证这确实是导致问题的原因。

要启动或停止 Windows 服务,您需要以管理权限运行 C# 程序。

这可以手动完成,方法是右键单击应用程序,然后单击“以管理员身份运行”

或者,如果您将以下代码添加到位于解决方案资源管理器项目的 Properties 文件夹中的 app.manifest 文件中,则可以通过将程序设置为始终以管理权限运行来以编程方式完成:

 <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
 <security>
 <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
 <!-- UAC Manifest Options
  If you want to change the Windows User Account Control level replace the 
  requestedExecutionLevel node with one of the following.

 <requestedExecutionLevel level="asInvoker" uiAccess="false" />
 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
 <requestedExecutionLevel level="highestAvailable" uiAccess="false" />

  If you want to utilize File and Registry Virtualization for backward 
  compatibility then delete the requestedExecutionLevel node.
 -->
 <requestedExecutionLevel level="asInvoker" uiAccess="false" />
 </requestedPrivileges>
 <applicationRequestMinimum>
 <defaultAssemblyRequest permissionSetReference="Custom" />
 <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
 </applicationRequestMinimum>
 </security>
 </trustInfo>
</asmv1:assembly>

您可以在此处找到有关创建和嵌入应用程序清单 (UAC) 的更多信息: https://msdn.microsoft.com/en-us/library/bb756929.aspx

【讨论】:

    【解决方案2】:

    Pranav,您需要以管理员权限运行您的应用程序。如果您以管理员身份运行 Visual Studio,您的应用程序应该可以在调试模式下正常工作。

    当你开始部署你的应用程序时,你既可以默认在管理员模式下运行它,也可以为你的应用程序分配适当的权限以使其能够启动和停止特定的服务(我相信这是在命令行上完成的) .我前段时间已经这样做了,快速的 Google 搜索会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2011-10-03
      • 2011-06-08
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多