[英]Overwrite a service using File System in C#
我开发了两个应用程序:
安装程序(安装服务)
升级(停止并覆盖已安装的服务)
运行升级时,出现异常
“该进程无法访问文件E:\\ TF50 \\ SSS Service \\ DataModel.dll,因为
它正在被另一个进程使用”。
这是我的示例代码:
try
{
if (xmlnode.Item(1).InnerText.ToString() != "")
{
ServiceController sc = new ServiceController();
sc.ServiceName = "RT60Service";
richTextBox1.Invoke((MethodInvoker)delegate
{
richTextBox1.Text = richTextBox1.Text + "RT60WindowService Started" +
" - " + DateTime.UtcNow + Environment.NewLine;
});
if (sc.Status == ServiceControllerStatus.Running)
sc.Stop();
fnUnZIP(xmlnode.Item(1).InnerText.ToString(), lstrHomeDirectory +
"RT60WindowsService", "RT60WindowService");
sc.Refresh();
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.Refresh();
}
richTextBox1.Invoke((MethodInvoker)delegate
{
richTextBox1.Text = richTextBox1.Text + "RT60WindowService Completed" + " - " +
DateTime.UtcNow + Environment.NewLine;
richTextBox1.Text = richTextBox1.Text + "---------------------------------------
---------------------------------------------------------" +
Environment.NewLine;
});
}
}
catch (Exception ex)
{
richTextBox1.Invoke((MethodInvoker)delegate
{
richTextBox1.Text = richTextBox1.Text + ex.Message + Environment.NewLine;
richTextBox1.Text = richTextBox1.Text + "RT60WindowService Failed" + " - " +
DateTime.UtcNow + Environment.NewLine;
richTextBox1.Text = richTextBox1.Text + "---------------------------------------
---------------------------------------------------------" +
Environment.NewLine;
});
}
finally
{
bgworker.ReportProgress(32);
}
现在我的问题是:“如何使用文件系统覆盖服务?”
我被卡在这里,请帮我..
我怀疑问题是当您尝试替换文件时,您的服务仍在关闭。
在你之后
sc.Stop();
您需要等待,直到使用该命令实际上停止了服务
sc.WaitForStatus(ServiceControllerStatus.Stopped);
要么
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.