Запрашиваемый доступ к реестру не разрешен. даже с подписанным exe и админом Привилегированный в манифесте - PullRequest
0 голосов
/ 29 мая 2019

Я пытаюсь получить доступ к реестру HKEY_LOCAL_MACHINE,

Это файл манифеста:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<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>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
    <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
    <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
    <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
</compatibility>

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
</application>
<dependency>
    <dependentAssembly>
    <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        processorArchitecture="*"
        publicKeyToken="6595b64144ccf1df"
        language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

Я также подписываю исполняемый файл тестовым сертификатом

это функция:

static string GetDeviceGuid()
{
    const string AdapterKey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
    RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey, RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey);
    string[] keyNames = regAdapters.GetSubKeyNames();
    string devGuid = "";
    foreach (string x in keyNames)
    {
        RegistryKey regAdapter = regAdapters.OpenSubKey(x);
        object id = regAdapter.GetValue("ComponentId");
        if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
    }
    return devGuid;
}

это результат:

at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at TestTun.TunTap.GetDeviceGuid()

что я должен сделать, чтобы прочитать реестр без исключения безопасности

1 Ответ

0 голосов
/ 29 мая 2019

Я обнаружил проблему, но не знаю, почему доступ к этому адресу невозможен

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\Properties

Ошибка реестра при Regedit Отладка Visual Studio

Я исправил свой код с помощью регулярных выражений

static string GetDeviceGuid()
    {
        const string AdapterKey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
        RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey);
        string[] keyNames = regAdapters.GetSubKeyNames();
        foreach (string x in keyNames) {
            Console.WriteLine(x);
        }
        string devGuid = "";
        Regex rx = new Regex(@"\d{4}",
      RegexOptions.Compiled | RegexOptions.IgnoreCase);
        foreach (string x in keyNames)
        {
            if (!rx.IsMatch(x)) {
                continue;
            } 
            RegistryKey regAdapter = regAdapters.OpenSubKey(x);
            object id = regAdapter.GetValue("ComponentId");
            if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
        }
        return devGuid;
    }
...