-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (46 loc) · 1.68 KB
/
Program.cs
File metadata and controls
51 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
namespace SyncClock
{
class Program
{
static bool IsW32Time(ServiceController service)
{
return service.ServiceName.Equals("W32Time", StringComparison.OrdinalIgnoreCase);
}
static void Main(string[] args)
{
var services = ServiceController.GetServices();
var w32time = services.FirstOrDefault(IsW32Time);
if(w32time != null)
{
try
{
var status = w32time.Status;
if(status != ServiceControllerStatus.Running &&
status != ServiceControllerStatus.StartPending &&
status != ServiceControllerStatus.ContinuePending)
{
w32time.Start();
}
w32time.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
if(w32time.Status == ServiceControllerStatus.Running)
{
var psi = new ProcessStartInfo("w32tm", "/resync");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
Process.Start(psi);
}
}
catch(Exception)
{
}
}
}
}
}