diff --git a/NotifyIconBuilder.cs b/NotifyIconBuilder.cs index 4c69d94..750dd7b 100644 --- a/NotifyIconBuilder.cs +++ b/NotifyIconBuilder.cs @@ -26,7 +26,7 @@ public NotifyIconBuilder Configure(Action builder) return this; } - public NotifyIcon Build(Icon icon) + public NotifyIcon Build(Icon? icon) { return new NotifyIcon { diff --git a/NotifyIconController.cs b/NotifyIconController.cs index c1fbd5f..43fd491 100644 --- a/NotifyIconController.cs +++ b/NotifyIconController.cs @@ -48,11 +48,24 @@ public NotifyIconController(ISettings settings) .AddButton(option => option .SetText("E&xit") .AddHandler(() => ExitClicked?.Invoke()))) - .Build(Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location)!); + .Build(GetIcon()); NotifyIcon.Text = "PowerDimmer"; NotifyIcon.Visible = true; } + + private static Icon? GetIcon() + { + var location = Assembly.GetExecutingAssembly().Location; + if (string.IsNullOrEmpty(location)) + { + location = Environment.ProcessPath; + } + + return !string.IsNullOrEmpty(location) + ? Icon.ExtractAssociatedIcon(location) + : null; + } } // https://stackoverflow.com/a/24825487