Reputation: 131
I've an application in c# 2010 on windows 7 64bit. I'm trying to start SQLBROWSER via this code :
public void Start()
{
if (_service.Status != ServiceControllerStatus.Running ||
_service.Status != ServiceControllerStatus.StartPending)
_service.Start();
_service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));
}
and I create an app.manifest file to run my app as Administrator.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
and here the error when I execute my app as administrator :
Cannot start service SQLBrowser on computer '.'
Upvotes: 1
Views: 899
Reputation: 131
Well, I found the solution for my problem, thank you so much @user3394380 for help, here the correct code to enable and start the service :
// 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();
NB : Don't enable and start the service in the same action, as I do, I enable it in Action A, and start it in Action B.
Upvotes: 1
Reputation: 943
I debug in VS 2010 started as Administrator. It works in compiled apps.
There are 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>
and code:
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));
}
}
Check MSDN article here http://technet.microsoft.com/en-us/library/cc739213(v=ws.10).aspx
Upvotes: 0