-
Notifications
You must be signed in to change notification settings - Fork 0
/
uTorrentService.cs
76 lines (61 loc) · 1.94 KB
/
uTorrentService.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace uTorrentService
{
using System.IO;
public partial class uTorrentService : ServiceBase
{
private Process process;
public uTorrentService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var exePath = GetExeLocation(args);
if (!File.Exists(exePath))
{
var message = string.Format("The file {0} could not be found.", exePath);
this.EventLog.WriteEntry(message, EventLogEntryType.Error);
Environment.Exit(1);
}
ProcessStartInfo psi = new ProcessStartInfo(exePath);
psi.UseShellExecute = true;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
try
{
this.process = Process.Start(psi);
if (this.process != null)
{
process.WaitForInputIdle();
}
}
catch (Exception ex)
{
var message = string.Format("Executing the utorrent.exe failed: {0}", ex);
this.EventLog.WriteEntry(message, EventLogEntryType.Error);
Environment.Exit(2);
}
}
protected override void OnStop()
{
process.Close();
process.Dispose();
}
private string GetExeLocation(string[] args)
{
if (args.Length != 0)
{
return args[0];
}
var sameDirPath = Path.Combine(Environment.CurrentDirectory, "uTorrent.exe");
if (File.Exists(sameDirPath))
{
return sameDirPath;
}
return Environment.ExpandEnvironmentVariables("%AppData%\\uTorrent\\uTorrent.exe");
}
}
}