Implement autostart functionality, ZIP packaging, and related installation scripts; update documentation and tests
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
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}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user