如何避免错误“无法在您的计算机上找到服务。拒绝访问”
问题描述:
我正在尝试从按钮单击的C#中的Windows应用程序界面启动Windows服务。它在我的机器上工作正常
但在另一台服务器上我收到错误无法在您的计算机上找到服务.System.ComponentModel.Win32Exception:Access被拒绝 。
我尝试过:
代码如下:
I am trying to start a windows service from a windows application interface in C# on button click. It is working fine on my machine
But on another server I am getting the error "cannot find service on your computer. System.ComponentModel.Win32Exception: Access is denied".
What I have tried:
Code is as below:
#region StartService
//To start Windows Service exe created and installed in Services.msc
public bool StartService(string serviceName)
{
ServiceController sc = new ServiceController(serviceName);
Archive AC = new Archive();
//String ErrorLogPath = System.Configuration.ConfigurationSettings.AppSettings["ErrorLogPath"].ToString();
string ErrorLogPath = ConfigurationSettings.AppSettings["ErrorLogPath"].ToString();
string ErrorFilename = ErrorLogPath + "WPS_Archive_ErrorFile" + ".txt";
try
{
if ((sc.Status.Equals(ServiceControllerStatus.Running)))
{
// Check whether the service if the current status is Running.
return true;
}
else
{
sc.Start();
return true;
}
}
catch (Exception ex)
{
AC.WriteTextFile(ErrorFilename, " Service not started " + Environment.NewLine + ex.ToString());
//}
MessageBox.Show("Service not started, " + ex.Message);
}
return false;
}
#endregion
答
您需要管理员权限才能安装服务。这意味着用于安装服务的应用程序必须以管理员身份运行。
You need admin permissions to install a service. This means the app being used to install the service must run as administrator.
运行应用程序的用户帐户无权启动服务。您必须以具有该权限的用户身份运行该应用程序。即:以管理员身份运行(可能是您系统中的情况),或者授予特定用户启动服务的权限。
The user account running your application does not have the permission to start services. You have to run the application as a user who does have the permission. That is: Run it as administrator (probably the case on your system) or give a specific user the permission to start services.