启动远程服务从C# [英] Start A Remote Service From C#

查看:163
本文介绍了启动远程服务从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在C#WinForms应用程序下面的代码到远程PC上启动Windows服务

I'm using the following code in a C# WinForms app to start windows services on a remote PC

    public static List<Service> GetServices()
    {
        List<Service> Services = new List<Service>();
        ServiceController[] sc = ServiceController.GetServices(Server);
        foreach (var s in sc)
        {
           Services.Add(new Service { Name = s.ServiceName, Running = s.Status == ServiceControllerStatus.Running });
        }
        return Services;
    }

    public static bool StartService(string ServiceName)
    {
        try
        {
            ServiceController sc = new ServiceController(ServiceName, Server);
            sc.Start();
            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
            sc.Refresh();
            return sc.Status == ServiceControllerStatus.Running;
        }
        catch(Exception ex) { return false; }
    }



GetServices方法在本地PC或远程指点时工作正常PC。然而,StartService方法只适用于本地PC上,当远程计算机上运行,​​我得到访问被拒绝。在这种情况下,远程PC是同一个域,我下运行上有本地管理员权限的应用程序,用户在Windows XP专业版的机器。

The GetServices method works fine when pointing at my local PC or at the remote PC. The StartService method however only works on my local PC, when run on the remote PC I get access denied. In this case the remote PC is a windows XP pro machine on the same domain and the user I'm running the app under has local admin rights on it.

我不能确定这是否是我的代码或一个问题,如果我的权限是不正确的。

I'm not sure if this is an issue with my code or if my permissions are not correct.

如果这是一个权限问题,请让我知道,我会试着问对ServerFault。

If this is a permissions issue please let me know and I'll try asking on ServerFault.

感谢

推荐答案

原来,这是一个权限问题。我是用虚拟机测试,我们的虚拟机对我们的域不同的访问权限。

It turns out this was a permissions issue. I was testing with a virtual machine and our VM's have different access permissions on our domain.

这篇关于启动远程服务从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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