Доступ к реестру Windows с помощью.NET?

Не будет работать на Вас, поскольку похоже, что Вы не управляете querystring, но другим действительным ответом: Вместо синтаксического анализа querystring, Вы могли appeand' []' до конца имени, тогда PHP сделает массив объектов.

IE:

someurl.php?name[]=aaa&name[]=bbb

даст Вам $ _GET сходство с:

array(0=>'aaa', 1=>'bbb')
6
задан Peter Mortensen 13 February 2015 в 19:44
поделиться

4 ответа

Blair's answer regarding reverting to the Win32 API, and calling the RegOpenKeyEx function within is about the best way of achieving this.

Windows itself will map specific registry locations into logical views using a Registry Redirector and Registry Reflection.

You can read more about this process in the following MSDN article:
32-bit and 64-bit Application Data in the Registry

Despite the Win32 API being the best way to achieve this, this is the possibility of "hard-coding" the location of the registry that you are seeking to retrieve, although this is fraught with possible problems (Microsoft themselves do not support this method). You can read more about this in this Stack Overflow Question:
How to open a WOW64 registry key from a 64-bit .NET application

Ultimately, the Win32 API seems about the best solution (if not the most elegant) at the moment.
Heath Stewart from Microsoft gives the following answer in this MSDN Social question:

Unfortunately there isn't a way to pass those flags to the managed registry APIs under the Microsoft.Win32 namespace. You would have to P/Invoke the native API such as RegCreateKeyEx that you mentioned.

Consider, however, if you need to store data in a 32- or 64-bit view of the registry. The topic Registry Redirector in MSDN has the keys that are redirected, which you're probably familiar with, and the topic Registry Reflection has the keys in which values are copied between 32- and 64-bit keys.

If indeed you need separate views, you might also consider enabling registry reflection for your keys if you desire both your 32- and 64-bit applications to share at least some registry data. See the documentation for RegEnableReflectionKey for more details.

2
ответ дан 8 December 2019 в 05:56
поделиться

I have had a similar problem, and the best answer I've found is to fall back to the Win32 Registry Functions (such as RegOpenKeyEx) and pass in the appropriate Registry Key Security and Access Rights, specifically OR'ing the samDesired parameter with theKEY_WOW64_64KEY.

It was awful, and I hope you hear a better answer here.

4
ответ дан 8 December 2019 в 05:56
поделиться

In the following code, GetAge() will return your key value, or null if the key doesn't exist.

[DllImport("Advapi32.dll")]
static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out int phkResult);
[DllImport("Advapi32.dll")]
static extern uint RegCloseKey(int hKey);
[DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
public static extern int RegQueryValueEx(int hKey, string lpValueName, int lpReserved, ref uint lpType, System.Text.StringBuilder lpData, ref uint lpcbData);

public const int KEY_QUERY_VALUE = 0x0001;
public const int KEY_WOW64_64KEY = 0x0100;

static public string GetAge()
{
    string EPG_REGKEY = @"SOFTWARE\Test\MyParameters";
    UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
    int hkey = 0;

    try
    {
        uint lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, EPG_REGKEY, 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, out hkey);
        if (0 != lResult) return null;
        uint lpType = 0;
        uint lpcbData = 1024;
        StringBuilder AgeBuffer = new StringBuilder(1024);
        RegQueryValueEx(hkey, "Age", 0, ref lpType, AgeBuffer, ref lpcbData);
        string Age = AgeBuffer.ToString();
        return Age;
    }
    finally
    {
        if (0 != hkey) RegCloseKey(hkey);
    }
}
2
ответ дан 8 December 2019 в 05:56
поделиться

Если вы можете изменить целевую версию .Net на v4, вы можете использовать новую функцию OpenBaseKey, например.

RegistryKey registryKey;
if (Environment.Is64BitOperatingSystem == true)
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
9
ответ дан 8 December 2019 в 05:56
поделиться
Другие вопросы по тегам:

Похожие вопросы: