18.12.2025 15:04 C#
Для використання бібліотек Gtk4 потрібно додати в системну змінну оточення Path - шлях до папки де розміщені бібілотеки (наприклад C:\msys64\ucrt64\bin).
Дана програма перевіряє чи вже заданий шлях до папки з бібліотеками в системну змінну оточення PATH і якщо потрібно добавляє.
dotnet new consoledotnet rundotnet buildusing System.Runtime.Versioning;
using Microsoft.Win32;
[SupportedOSPlatform("windows")]
class Program
{
static void Main()
{
string regKeyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
string newPath = "";
string findPath = @"C:\msys64\ucrt64\bin".Trim().ToLower();
//Перевірка останнього символу шляху для пошуку і видалення слешу
if (findPath.EndsWith('\\') || findPath.EndsWith('/'))
findPath = findPath[..^1];
bool update = true;
//Пошук в реєстрі
RegistryKey? key = Registry.LocalMachine.OpenSubKey(regKeyName);
string? value = (string?)key?.GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
if (value != null)
{
//Розбивка записів на частини
string[] paths = value.Split([';'], StringSplitOptions.RemoveEmptyEntries);
foreach (string path in paths)
{
//Формування нового значення
newPath += path + ";";
//Перевірка наявності запису
if (path.ToLower() == findPath)
update = false;
}
if (update == true)
try
{
//Запис нового значення
Environment.SetEnvironmentVariable("Path", newPath + findPath, EnvironmentVariableTarget.Machine);
Console.WriteLine($"OK. Path {findPath} added");
}
catch (System.Security.SecurityException)
{
Console.WriteLine("Помилка: Необхідні права адміністратора для запису системної змінної.");
}
catch (Exception ex)
{
Console.WriteLine($"Сталася помилка: {ex.Message}");
}
else
Console.WriteLine($"OK. Path {findPath} exist");
}
Console.WriteLine($"Press Enter to exit program");
Console.ReadLine();
}
}<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.0.0.0</Version>
<Authors>Tarachom</Authors>
<Company>Tarachom</Company>
<Copyright>Copyright © 2025 Tarachom</Copyright>
<Description>Tarachom</Description>
</PropertyGroup>
</Project><?xml version="1.0" encoding="utf-8" ?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>© accounting.org.ua - 2025