Skip to content

Instantly share code, notes, and snippets.

@H3mnz
Last active August 28, 2024 11:08
Show Gist options
  • Save H3mnz/8e6494853e17a37a18ebc2193e0ba88f to your computer and use it in GitHub Desktop.
Save H3mnz/8e6494853e17a37a18ebc2193e0ba88f to your computer and use it in GitHub Desktop.
Install Font in Windows with Dart
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:path/path.dart';
import 'package:win32_registry/win32_registry.dart';
class FontInstaller {
FontInstaller._();
static Future<void> install(String path) async {
try {
final windowsDirectory = Platform.environment['WINDIR'];
if (windowsDirectory == null) throw Exception('WINDIR is not exists in Platform.environment');
final newPath = join(windowsDirectory, 'Fonts', basename(path));
final file = await File(path).copy(newPath);
if (!file.existsSync()) throw Exception('Cant copy font to $newPath');
const keyPath = r'Software\Microsoft\Windows NT\CurrentVersion\Fonts';
final registryKey = Registry.openPath(RegistryHive.localMachine, path: keyPath);
final fontValue = RegistryValue(
basenameWithoutExtension(path),
RegistryValueType.string,
basename(path),
);
registryKey.createValue(fontValue);
registryKey.close();
} catch (e) {
log(e.toString());
rethrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment