-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinuxAutoStartService.cs
More file actions
63 lines (53 loc) · 1.85 KB
/
Copy pathLinuxAutoStartService.cs
File metadata and controls
63 lines (53 loc) · 1.85 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
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace SshAgentEcho.Gui.Autostart {
public class LinuxAutoStartService : IAutostartService {
private readonly string _autostartDirectory;
private readonly string _appName;
// factory passes appName in codebase; keep constructor to match that usage even though methods take appName.
public LinuxAutoStartService(string appName) {
_appName = appName;
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
_autostartDirectory = Path.Combine(home, ".config", "autostart");
}
private string DesktopFilePath(string appName) => Path.Combine(_autostartDirectory, $"{appName}.desktop");
public bool Install() {
try {
Directory.CreateDirectory(_autostartDirectory);
var exec = ((IAutostartService)this).GetExecutablePath();
var quotedExec = exec.Contains(" ") ? $"\"{exec}\"" : exec;
var desktopFile = $@"
[Desktop Entry]
Type=Application
Name={_appName}
Exec={quotedExec}
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true";
File.WriteAllText(DesktopFilePath(_appName), desktopFile);
return true;
} catch {
return false;
}
}
public bool Uninstall() {
try {
var path = DesktopFilePath(_appName);
if (File.Exists(path)) File.Delete(path);
return true;
} catch {
return false;
}
}
public bool IsInstalled() {
try {
var path = DesktopFilePath(_appName);
return File.Exists(path);
} catch {
return false;
}
}
}
}