-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunOnStartupManagerLinux.cs
More file actions
65 lines (58 loc) · 2.49 KB
/
RunOnStartupManagerLinux.cs
File metadata and controls
65 lines (58 loc) · 2.49 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
64
65
using System;
using System.IO;
namespace RunOnStartup
{
internal class RunOnStartupManagerLinux : IRunOnStartupManager
{
private const string ALL_USERS_STARTUP_PATH = "/etc/xdg/autostart/";
private const string CURRENT_USER_STARTUP_PATH = "%HOME%/.config/autostart/";
private const string DESKTOP_FILE_EXTENSION = ".desktop";
private const string DESKTOP_FILE_TEMPLATE =
"[Desktop Entry]\n" +
"Type=Application\n" +
"Name={0}\n" +
"Exec={1}\n" +
"NoDisplay=true\n";
private static string GetStartupFolderPath(bool allUsers)
{
return Environment.ExpandEnvironmentVariables(allUsers ? ALL_USERS_STARTUP_PATH : CURRENT_USER_STARTUP_PATH);
}
private static string GetDesktopFilePath(string startupFolderPath, string uniqueName)
{
return Path.Combine(startupFolderPath, uniqueName + DESKTOP_FILE_EXTENSION);
}
private static string BuildExecCommandLine(string programPath, string? escapedArguments)
{
string execCommandline = $"\"{programPath}\"";
if (escapedArguments != null)
{
execCommandline += " " + escapedArguments;
}
return execCommandline;
}
public bool IsRegistered(string uniqueName, bool allUsers)
{
string startupFolderPath = GetStartupFolderPath(allUsers);
return File.Exists(GetDesktopFilePath(startupFolderPath, uniqueName));
}
public void Register(string uniqueName, string programPath, string? arguments, bool allUsers)
{
string startupFolderPath = GetStartupFolderPath(allUsers);
Directory.CreateDirectory(startupFolderPath);
string desktopFilePath = GetDesktopFilePath(startupFolderPath, uniqueName);
string execCommandline = BuildExecCommandLine(Path.GetFullPath(programPath), arguments);
File.WriteAllText(desktopFilePath, string.Format(DESKTOP_FILE_TEMPLATE, uniqueName, execCommandline));
}
public bool Unregister(string uniqueName, bool allUsers)
{
string startupFolderPath = GetStartupFolderPath(allUsers);
string desktopFilePath = GetDesktopFilePath(startupFolderPath, uniqueName);
if (File.Exists(desktopFilePath))
{
File.Delete(desktopFilePath);
return true;
}
return false;
}
}
}