2
레지스트리 키를 읽지 만 실패하는 다음 코드 비트에 문제가 있습니다. 특정 오류 : "System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되어 있지 않습니다." 내가 사용하는 코드는 다음과 같습니다 : 내 환경에 대한Windows 7에서 64 비트 레지스트리 키를 읽을 수 없습니다.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Collections;
using System.Diagnostics;
using System.Security.Principal;
namespace UDTLibrary
{
public class NotificationBar
{
public static void Main(string[] args)
{
//Get User Info
string sSource;
string sLog;
sSource = "TestCSFileSysWatcher";
sLog = "Application";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, "NotificationBar.Main start");
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
EventLog.WriteEntry(sSource, "NotificationBar.Main - non-Administrator");
}
else
{
EventLog.WriteEntry(sSource, "NotificationBar.Main Administrator");
}
NotificationBar p1 = new NotificationBar();
string prName = null;
int value = 0;
if (args == null)
{
throw new Exception("Attempt to run NotificationBar with no arguments supplied.");
}
else
{
if (args.Length != 2)
{
throw new Exception("Wrong number of arguments supplied.");
}
else
{
prName = args[0];
value = Convert.ToInt32(args[1]);
}
}
RegistryKey currentUser = null;
if (Environment.Is64BitOperatingSystem)
{
currentUser = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64);
}
else
{
currentUser = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
}
RegistryKey myKey = currentUser.OpenSubKey(@"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify", true);
byte[] all = (byte[])myKey.GetValue("IconStreams"); //here is where the code fails
byte[] allwithoutheader = new byte[all.Length - 20];
byte[] header = new byte[20];
몇 가지 사실 :
- 이 내가 윈도우 7에서 실행하고있어 32 비트 응용 프로그램입니다 (UAC가 활성화 된 상태 - 아니, 나는 그것을 끌 수 없다.) 그러나 위의 코드에서 볼 수 있듯이 레지스트리의 64 비트보기에서 읽는 중입니다. RegistryView.Registry64가 선택되었음을 확인했습니다.
- 코드가 관리 권한으로 실행되고 있습니다. 위의 코드를 통해 WindowsBuiltInRole.Administrator를 확인한 결과 로그가 "관리자가 아닌"줄이 아닌 "관리자"줄을 사용합니다.
- 바이트가 아닌 문자열을 읽도록 코드를 변경하려고 시도했습니다. 또한 성공하지 못한 다른 위치 (HKCU가 아닌 HKLM)에서 읽을 수 있습니다.
내가 여기에 분명한 것을 놓치고 있습니까? 당신이 줄 수있는 충고는 대단히 감사하겠습니다. 문제를 해결해야 할 것이 있으면 알려주세요.
봐를 입력하고자하는 경우 bably currentKey 또는 myKey가 null입니다. – makc
감사합니다. myKey는 실제로 null이며 (currentUser는 아닙니다.). 그러나 HKCU \ Software \ Classes \ Local Settings \ Software \ Microsoft \ Windows \ CurrentVersion \ TrayNotify 키가 실제로 존재한다는 것을 확인했습니다. 왜 내 코드가 그것을 볼 수 없습니까? – user2300159
현재 사용자가 HKCU입니까? – makc