35 lines
1.6 KiB
C#
35 lines
1.6 KiB
C#
using Microsoft.Win32;
|
|
|
|
namespace MusicBridge.Agent.Desktop;
|
|
|
|
internal sealed class StartupSettings(string executable, bool lan, int port,
|
|
string registryPath = @"Software\Microsoft\Windows\CurrentVersion\Run")
|
|
{
|
|
private const string ValueName = "MusicBridge.Agent";
|
|
public string Command => BuildCommand(executable, lan, port);
|
|
public bool Enabled
|
|
{
|
|
get
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(registryPath);
|
|
return string.Equals(key?.GetValue(ValueName) as string, Command, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
public void SetEnabled(bool enabled)
|
|
{
|
|
if (enabled && !File.Exists(executable)) throw new FileNotFoundException("Сначала установи или опубликуй MusicBridge.", executable);
|
|
using var key = Registry.CurrentUser.CreateSubKey(registryPath);
|
|
if (enabled) key.SetValue(ValueName, Command, RegistryValueKind.String);
|
|
else key.DeleteValue(ValueName, throwOnMissingValue: false);
|
|
}
|
|
|
|
internal static string BuildCommand(string executable, bool lan, int port)
|
|
{
|
|
if (!Path.IsPathFullyQualified(executable) || executable.Contains('"') || executable.Any(char.IsControl)
|
|
|| !Path.GetFileName(executable).Equals("MusicBridge.Agent.exe", StringComparison.OrdinalIgnoreCase)
|
|
|| port is < 1 or > 65535) throw new ArgumentException("Неверный путь или порт автозапуска.");
|
|
return $"\"{executable}\" --tray --minimized{(lan ? " --lan" : "")} --port {port}";
|
|
}
|
|
}
|