我在Windows 7 64bit上的c#2010中有一个应用程序。 我正试图通过以下代码启动SQLBROWSER:
public void Start()
{
if (_service.Status != ServiceControllerStatus.Running ||
_service.Status != ServiceControllerStatus.StartPending)
_service.Start();
_service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));
}
我创建了一个app.manifest文件,以管理员身份运行我的应用程序。
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
这是我以管理员身份执行我的应用时的错误:
无法在计算机上启动服务SQLBrowser'。'
答案 0 :(得分:1)
好吧,我找到了我的问题的解决方案,非常感谢@ user3394380的帮助,这里有正确的代码来启用和启动服务:
// Enable the service :
// Create a .cmd file and write the code below, then launch it via a process
"SC \\" + System.Environment.MachineName + @" Config SQLBROWSER start= auto"
// Start the service :
// Call Start()
_service.Start();
注意:不要像我一样在同一个操作中启用和启动服务,我在Action A中启用它,然后在Action B中启动它。
答案 1 :(得分:0)
我在VS 2010中以管理员身份启动调试。它适用于已编译的应用程序。
有app.manifest:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
</application>
</compatibility>
</asmv1:assembly>
和代码:
class Program
{
static ServiceController _service = new ServiceController("SQLBROWSER");
static void Main(string[] args)
{
//Enable service before starting it.
Process.Start("sc.exe", " config SQLBROWSER start=auto");
Start();
}
static void Start()
{
if (!(_service.Status == ServiceControllerStatus.Running || _service.Status == ServiceControllerStatus.StartPending))
_service.Start();
_service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));
}
}
在此处查看MSDN文章http://technet.microsoft.com/en-us/library/cc739213(v=ws.10).aspx