Last active
August 28, 2024 11:08
-
-
Save H3mnz/8e6494853e17a37a18ebc2193e0ba88f to your computer and use it in GitHub Desktop.
Install Font in Windows with Dart
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
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