您好,我试图通过.NET应用程序启动/停止/禁用windows服务,但我似乎无法获得以下代码工作,它一直在说错误4错误C2065:'ServiceController‘:未声明的标识符
这方面的正确参考是什么?我似乎在系统中找不到正确的:
String ^servicename = "srservice";
// Stop the service if it's started.
ServiceController^ controller = new ServiceController(servicename);
if (controller.Status == ServiceControllerStatus.Running)
controller.Stop();
// Set the startup type of the service.
String^ serviceregistrylocation = String::Format("SYSTEM\CurrentControlSet\Services\{0}", servicename);
RegistryKey ^localMachine = Registry::LocalMachine;
RegistryKey servicekey = localMachine.OpenSubKey(serviceregistrylocation, true);
// Set value to 2 for automatic, 3 for manual, or 4 for disabled.
servicekey.SetValue("Start", 3); 好的,所以我修改了代码,它现在编译了,但是抛出了一个"object reference not set to an object instance of an object“错误
String ^servicename = "Fax";
// Stop the service if it's started.
ServiceController^ controller = gcnew ServiceController(servicename);
if (controller->Status == ServiceControllerStatus::Running)
controller->Stop();
// Set the startup type of the service.
String^ serviceregistrylocation = String::Format("SYSTEM\CurrentControlSet\Services\{0}", servicename);
RegistryKey ^localMachine = Registry::LocalMachine;
RegistryKey ^servicekey = localMachine->OpenSubKey(serviceregistrylocation, true);
// Set value to 2 for automatic, 3 for manual, or 4 for disabled.
try{
servicekey->SetValue("Start", 4);
}
catch ( Exception^ e )
{
MessageBox::Show( e->Message );
}
}发布于 2011-07-19 04:05:09
你有System::ServiceProcess的using语句吗?如果不是,则必须使用System::ServiceProcess::ServiceController完全限定该类。此外,还必须包含对System.ServiceProcess.dll程序集的引用。您可以通过从项目的右键单击上下文菜单中选择Properties来添加引用。然后从左侧树视图的顶部选择"Common Properties“。单击“添加新引用...”按钮,然后选择适当的引用。或者,您可以右键单击项目并选择“引用...”

https://stackoverflow.com/questions/6738649
复制相似问题