|
1 | 1 | # AutoLaunch |
2 | 2 |
|
3 | | -[AutoLaunch](https://github.com/Linlccc/AutoLaunch) provides a cross-platform(Windows, Linux, and macOS) feature for automatically launching applications or executable files at login, suitable for .NET. |
| 3 | +<img alt="AutoLaunch" src="https://raw.githubusercontent.com/Linlccc/AutoLaunch/master/docs/icon/icon.png" width="128"> |
| 4 | + |
| 5 | +English | [简体中文](README-ZH_CN.md) |
| 6 | + |
| 7 | +[](https://www.nuget.org/packages/AutoLaunch) |
| 8 | +[](https://www.nuget.org/packages/AutoLaunch) |
| 9 | +[](https://github.com/Linlccc/AutoLaunch/blob/master/LICENSE) |
| 10 | + |
| 11 | +[AutoLaunch](https://github.com/Linlccc/AutoLaunch) is a cross-platform .NET library that provides a unified API for enabling auto-start for applications and executables on Windows, Linux, and macOS systems. |
| 12 | + |
| 13 | +## ✨ Features |
| 14 | + |
| 15 | +- 🌍 **Cross-Platform Support:** Windows, Linux, macOS |
| 16 | +- 🔧 **Multiple Engines:** Various implementations for each platform |
| 17 | +- 🎯 **Ease of Use:** Unified API across all platforms |
| 18 | +- 🛠 **AOT Support:** Fully supports AOT and trimming |
| 19 | +- 📦 **Zero Dependency:** No third-party library required |
| 20 | + |
| 21 | +## 🚚 Supported Engines |
| 22 | + |
| 23 | +### Windows |
| 24 | + |
| 25 | +| Engine | Description | Permission | Note | |
| 26 | +|-------------------|-----------------------------------|------------|--------------------------------------------| |
| 27 | +| **Registry** | Manage startup via registry | User/Admin | | |
| 28 | +| **StartupFolder** | Manage startup via startup folder | User/Admin | | |
| 29 | +| **TaskScheduler** | Manage startup via Task Scheduler | Admin | Can launch programs requiring admin rights | |
| 30 | + |
| 31 | +### Linux |
| 32 | + |
| 33 | +| Engine | Description | Permission | Note | |
| 34 | +|-----------------|-----------------------------------------|------------|-------------------------------------------------------| |
| 35 | +| **Freedesktop** | Manage startup via Freedesktop standard | User/Admin | Requires a desktop environment supporting Freedesktop | |
| 36 | + |
| 37 | +### macOS |
| 38 | + |
| 39 | +| Engine | Description | Permission | Note | |
| 40 | +|-----------------|--------------------------------------|-----------------------|-------------------------------------------------| |
| 41 | +| **LaunchAgent** | Manage startup via Launch Agent | User/Admin | | |
| 42 | +| **AppleScript** | Manage startup items via login items | Automation permission | Only supports `--hidden`/`--minimize` arguments | |
| 43 | + |
| 44 | +## 📦 Installation |
| 45 | + |
| 46 | +dotnet CLI: |
| 47 | + |
| 48 | +```bash |
| 49 | +dotnet add package AutoLaunch |
| 50 | +``` |
| 51 | + |
| 52 | +Package Manager Console: |
| 53 | + |
| 54 | +```powershell |
| 55 | +Install-Package AutoLaunch |
| 56 | +``` |
| 57 | + |
| 58 | +## 🚀 Quick Start |
| 59 | + |
| 60 | +Unified configuration API for all platforms |
| 61 | + |
| 62 | +### Basic Usage |
| 63 | + |
| 64 | +```csharp |
| 65 | +using AutoLaunch; |
| 66 | + |
| 67 | +// Automatically configure for current program |
| 68 | +var autoLauncher = new AutoLaunchBuilder().Automatic().Build(); |
| 69 | + |
| 70 | +// Enable auto-launch synchronously |
| 71 | +autoLauncher.Enable(); |
| 72 | +// Disable auto-launch synchronously |
| 73 | +autoLauncher.Disable(); |
| 74 | +// Check if enabled synchronously |
| 75 | +bool isEnabled = autoLauncher.IsEnabled(); |
| 76 | + |
| 77 | +// Enable auto-launch asynchronously |
| 78 | +await autoLauncher.EnableAsync(); |
| 79 | +// Disable auto-launch asynchronously |
| 80 | +await autoLauncher.DisableAsync(); |
| 81 | +// Check if enabled asynchronously |
| 82 | +bool isEnabledAsync = await autoLauncher.IsEnabledAsync(); |
| 83 | +``` |
| 84 | + |
| 85 | +### Custom Configuration |
| 86 | + |
| 87 | +```csharp |
| 88 | +var autoLauncher = new AutoLaunchBuilder() |
| 89 | + .SetAppName("MyApp") |
| 90 | + .SetAppPath("/path/to/myapp") |
| 91 | + .SetArgs("arg1", "arg2") |
| 92 | + .AddArgs("arg3") |
| 93 | + .SetWorkScope(WorkScope.CurrentUser) // Set work scope for auto-launch |
| 94 | + .SetWindowsEngine(WindowsEngine.Registry) // Use Registry engine on Windows, ignored on other platforms |
| 95 | + .SetLinuxEngine(LinuxEngine.Freedesktop) // Use Freedesktop engine on Linux, ignored on other platforms |
| 96 | + .SetMacOSEngine(MacOSEngine.LaunchAgent) // Use LaunchAgent engine on macOS, ignored on other platforms |
| 97 | + .SetIdentifiers("com.example.myapp") // Add Bundle Identifier for macOS |
| 98 | + .SetExtraConfigIf(OperatingSystem.IsLinux(), "X-GNOME-Autostart-enabled=true") // Add extra config for Linux, must conform to Freedesktop standard |
| 99 | + .SetExtraConfigIf(OperatingSystem.IsMacOS(), "<key>KeepAlive</key><true/>") // Add extra config for macOS, must conform to LaunchAgent standard |
| 100 | + .Build(); |
| 101 | + |
| 102 | +autoLauncher.Enable(); |
| 103 | +``` |
| 104 | + |
| 105 | +### Safe Mode |
| 106 | + |
| 107 | +No exceptions will be thrown in safe mode |
| 108 | + |
| 109 | +```csharp |
| 110 | +// Build an instance in safe mode |
| 111 | +var autoLauncher = new AutoLaunchBuilder().Automatic().BuildSafe(); |
| 112 | + |
| 113 | +// Try to enable, returns true/false for success/failure |
| 114 | +bool success = autoLauncher.TryEnable(); |
| 115 | + |
| 116 | +if(!success) |
| 117 | +{ |
| 118 | + // Get the last exception |
| 119 | + Exception? lastException = autoLauncher.TakeLastException(); |
| 120 | + if (lastException is PermissionDeniedException) Console.WriteLine("Permission denied."); |
| 121 | + else Console.WriteLine($"Failed to enable auto-launch: {lastException?.Message}"); |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## API Documentation |
| 126 | + |
| 127 | +### AutoLaunchBuilder |
| 128 | + |
| 129 | +| Method | Description | |
| 130 | +|-----------------------------------|-------------------------------------------| |
| 131 | +| `Automatic()` | Automatically configure app name and path | |
| 132 | +| `SetAppName(string)` | Set app name | |
| 133 | +| `SetAppPath(string)` | Set app path | |
| 134 | +| `SetArgs(params string[])` | Set startup arguments | |
| 135 | +| `AddArgs(params string[])` | Add startup arguments | |
| 136 | +| `SetWorkScope(WorkScope)` | Set work scope (current user/all users) | |
| 137 | +| `SetWindowsEngine(WindowsEngine)` | Set Windows engine | |
| 138 | +| `SetLinuxEngine(LinuxEngine)` | Set Linux engine | |
| 139 | +| `SetMacOSEngine(MacOSEngine)` | Set macOS engine | |
| 140 | +| `SetIdentifiers(params string[])` | Set identifiers (macOS LaunchAgent only) | |
| 141 | +| `AddIdentifiers(params string[])` | Add identifiers (macOS LaunchAgent only) | |
| 142 | +| `SetExtraConfig(string)` | Set extra configuration | |
| 143 | +| `SetExtraConfigIf(bool, string)` | Conditionally set extra configuration | |
| 144 | +| `Build()` | Build AutoLauncher instance | |
| 145 | +| `BuildSafe()` | Build SafeAutoLauncher instance | |
| 146 | + |
| 147 | +### AutoLauncher Interface |
| 148 | + |
| 149 | +| Method | Description | |
| 150 | +|--------------------|------------------------------------| |
| 151 | +| `Enable()` | Enable auto-launch | |
| 152 | +| `Disable()` | Disable auto-launch | |
| 153 | +| `IsEnabled()` | Check if enabled | |
| 154 | +| `EnableAsync()` | Enable auto-launch asynchronously | |
| 155 | +| `DisableAsync()` | Disable auto-launch asynchronously | |
| 156 | +| `IsEnabledAsync()` | Check if enabled asynchronously | |
| 157 | + |
| 158 | +### SafeAutoLauncher Extra Methods |
| 159 | + |
| 160 | +| Method | Description | |
| 161 | +|-----------------------|-------------------------------------------------| |
| 162 | +| `TryEnable()` | Try to enable, returns success/failure | |
| 163 | +| `TryDisable()` | Try to disable, returns success/failure | |
| 164 | +| `TryIsEnabled()` | Try to check status, returns (success, enabled) | |
| 165 | +| `TryEnableAsync()` | Try to enable asynchronously | |
| 166 | +| `TryDisableAsync()` | Try to disable asynchronously | |
| 167 | +| `TryIsEnabledAsync()` | Try to check status asynchronously | |
| 168 | +| `TakeLastException()` | Get last exception | |
| 169 | + |
| 170 | +## ⚠️ Exception Types |
| 171 | + |
| 172 | +| Exception | Description | |
| 173 | +|------------------------------|------------------------------| |
| 174 | +| `AutoLaunchException` | Base exception class | |
| 175 | +| `AutoLaunchBuilderException` | Builder configuration error | |
| 176 | +| `UnsupportedOSException` | Unsupported operating system | |
| 177 | +| `PermissionDeniedException` | Permission denied | |
| 178 | +| `ExecuteCommandException` | Command execution failed | |
| 179 | + |
| 180 | +## 📜 License |
| 181 | + |
| 182 | +Licensed under the terms of the [MIT License](LICENSE). |
0 commit comments