C# 以静默方式更新Windows服务
我已经建立了一个Windows服务,现在我想让它自动更新。我读过一篇关于创建第二个服务或其他程序的文章,不能使用click one,那怎么办?有人知道吗?最好的方法是什么?我能换个组件吗C# 以静默方式更新Windows服务,c#,.net,windows-services,auto-update,C#,.net,Windows Services,Auto Update,我已经建立了一个Windows服务,现在我想让它自动更新。我读过一篇关于创建第二个服务或其他程序的文章,不能使用click one,那怎么办?有人知道吗?最好的方法是什么?我能换个组件吗 下载新的exe和任何附加程序集 重命名现有程序集的 复制新程序集的 重新启动服务。您可以将服务重启功能构建到主服务exe中 当服务启动时,从步骤2检查重命名的文件并删除它们以进行清理 要重新启动服务,请执行以下操作: System.Diagnostics.Process.Start (System.Re
System.Diagnostics.Process.Start
(System.Reflection.Assembly.GetEntryAssembly().Location)
private const string _mutexId = "MyUniqueId";
private static Mutex _mutex;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
bool alreadyRunning = false;
try
{
Mutex.OpenExisting(_mutexId);
alreadyRunning = true;
}
catch (WaitHandleCannotBeOpenedException)
{
alreadyRunning = false;
}
catch
{
alreadyRunning = true;
}
if (alreadyRunning)
{
using (ServiceController sc = new ServiceController("MyServiceName"))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 120));
sc.Start();
}
return;
}
}
catch
{
}
_mutex = new Mutex(true, _mutexId);
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
// Load the service into memory.
ServiceBase.Run(ServicesToRun);
_mutex.Close();
}
那么在你的服务中呢
System.Diagnostics.Process.Start
(System.Reflection.Assembly.GetEntryAssembly().Location)
private const string _mutexId = "MyUniqueId";
private static Mutex _mutex;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
bool alreadyRunning = false;
try
{
Mutex.OpenExisting(_mutexId);
alreadyRunning = true;
}
catch (WaitHandleCannotBeOpenedException)
{
alreadyRunning = false;
}
catch
{
alreadyRunning = true;
}
if (alreadyRunning)
{
using (ServiceController sc = new ServiceController("MyServiceName"))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 120));
sc.Start();
}
return;
}
}
catch
{
}
_mutex = new Mutex(true, _mutexId);
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
// Load the service into memory.
ServiceBase.Run(ServicesToRun);
_mutex.Close();
}
private const string\u mutexId=“MyUniqueId”;
专用静态互斥锁(Mutex);;
///
///应用程序的主要入口点。
///
静态void Main()
{
尝试
{
bool alreadyRunning=false;
尝试
{
Mutex.OpenExisting(_mutexId);
alreadyRunning=true;
}
捕获(WaitHandleCannotBeOpenedException)
{
alreadyRunning=false;
}
抓住
{
alreadyRunning=true;
}
如果(已运行)
{
使用(ServiceController sc=newServiceController(“MyServiceName”))
{
sc.停止();
sc.WaitForStatus(ServiceControllerStatus.Stopped,新的时间跨度(0,0,120));
sc.Start();
}
返回;
}
}
抓住
{
}
_互斥量=新互斥量(true,_mutexId);
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{
新MyService()
};
//将服务加载到内存中。
ServiceBase.Run(ServicesToRun);
_mutex.Close();
}
您可以修改Windows服务,使其成为主应用程序的运行程序,并具有更新主应用程序的功能
所以你会:
- Service.exe:运行Application.exe,监视远程位置以获取对Application.exe的更新。将启动/停止事件发送到Application.exe
- Application.exe:以前是您的Service.exe。接收启动/停止事件
…对不起,可能解释得不太清楚。在凌晨3点写答案可能不是最好的主意;)