I'm trying to delete a directory used by a service. Because the directory is used by the service. That is why I have to stop the service. I can start or stop the service by the following code.
static void ToggleHostService(HostStatus serviceStatus)
{
var hostServiceName = "ServiceHost";
if (serviceStatus == HostStatus.run)
{
using (var controller = new ServiceController(hostServiceName))
{
if (controller.Status != ServiceControllerStatus.Running)
controller.Start();
controller.Refresh();
}
}
else if (serviceStatus == HostStatus.stop)
{
using (var controller = new ServiceController(hostServiceName))
{
if (controller.Status != ServiceControllerStatus.Stopped)
controller.Stop();
controller.Refresh();
}
}
}
But when I'm trying to delete the directory, I get exception as
Unhandled Exception: System.IO.IOException: The process cannot access the file ' backup.wal' because it is being used by another process.
I can see in Service manager window that the service is really stopped. But why it still complains it can't access. To make sure I run the app/code in Admin mode. I tried Please tell me how can I really force delete that directory.