Last active
August 10, 2021 17:51
-
-
Save meitinger/355ea6eb0dbf2773a22788427301d245 to your computer and use it in GitHub Desktop.
Utility that starts Anki with its base set to an USB drive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright (C) 2021, Manuel Meitinger | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#nullable enable | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
static class Program | |
{ | |
[DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] | |
static extern bool AllowSetForegroundWindow(int dwProcessId); | |
static void LaunchAtDrive(DriveInfo drive) | |
{ | |
var psi = new ProcessStartInfo() | |
{ | |
FileName = Path.Combine(Application.StartupPath, "anki.exe"), | |
UseShellExecute = false, | |
}; | |
psi.EnvironmentVariables.Add("ANKI_BASE", Path.Combine(drive.Name, "Anki")); | |
using var process = Process.Start(psi); | |
AllowSetForegroundWindow(process.Id); | |
} | |
static DriveInfo? PickDrive(IEnumerable<DriveInfo> drives) | |
{ | |
var selectedDrive = (DriveInfo?)null; | |
var form = new Form() | |
{ | |
AutoSize = true, | |
ClientSize = new Size(0, 0), | |
FormBorderStyle = FormBorderStyle.FixedToolWindow, | |
StartPosition = FormStartPosition.CenterScreen, | |
Text = "USB-Stick für Anki auswählen...", | |
}; | |
var panel = new FlowLayoutPanel() | |
{ | |
AutoSize = true, | |
FlowDirection = FlowDirection.TopDown, | |
Margin = new Padding(0, 0, 0, 0), | |
Size = new Size(0, 0), | |
WrapContents = false, | |
}; | |
foreach (var drive in drives) | |
{ | |
var button = new Button() | |
{ | |
Text = $"{drive.VolumeLabel} ({drive.Name})", | |
UseVisualStyleBackColor = true, | |
Width = 200, | |
}; | |
button.Click += (_, _) => | |
{ | |
selectedDrive = drive; | |
form.Close(); | |
}; | |
panel.Controls.Add(button); | |
} | |
form.Controls.Add(panel); | |
form.ShowDialog(); | |
return selectedDrive; | |
} | |
public static void Main() | |
{ | |
try | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
var drives = DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType == DriveType.Removable); | |
var drive = drives.Count() switch | |
{ | |
0 => throw new DriveNotFoundException("Es wurde kein USB-Stick gefunden."), | |
1 => drives.Single(), | |
_ => PickDrive(drives), | |
}; | |
if (drive is not null) | |
{ | |
LaunchAtDrive(drive); | |
} | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.Message, e.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment