2016-08-29 3 views
0

C#WinFormsアプリケーションでRegisteredOwnerとRegisteredOrganizationの値を取得しようとしていて、デフォルト値を未知数として返しています。C#で登録された所有者を取得すると、未知の値が返される

string Owner = ""; 
     string Company = ""; 

     OperatingSystem osInfo = System.Environment.OSVersion; 
     if (osInfo.Platform == PlatformID.Win32Windows) 
     { 
      // Windows 98             
      Owner = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOwner", "Unknown"); 
      Company = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOrganization", "Unknown"); 
     } 
     else if (osInfo.Platform == PlatformID.Win32NT) 
     { 
      // for NT+     
      Owner = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOwner", "Unknown"); 
      Company = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization", "Unknown");    
      // do you need admin to read? or just write? b/c you need the UAC elevation. so out it on the Save button if it is just save changes, or in manifest.    
     } 

     lblRegOwner.Text = Owner;    
     lblRegOrg.Text = Company; 

同じ結果を持つ管理者としてプログラムを実行しようとしましたが、標準のコマンドプロンプトを使用して、返された値を取得できました。

C:\Users\jweinraub>reg query "HKLM\Software\Microsoft\windows nt\currentversion" /v registeredowner

HKEY_LOCAL_MACHINE\Software\Microsoft\windows nt\currentversion registeredowner REG_SZ Jonathan Weinraub

+0

https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.openbasekey(v=vs .110).aspx –

+0

どのバージョンのウィンドウですか?ちょうどWindows 7 x64、x86、コピーを貼り付けて、これをテストした.net 4.5.1を使っているcpuビルドは正しく動作する。 – bgura

+0

Windows 10 x64は私が現在実行している場所です。興味深いことに、私はちょうどそれをx64にしました、そして、それはトリックをやったようです。しかし、実際にはそれを必要とするプログラムではないので、別のバイナリを維持することなくどちらのアーキテクチャでも動作するようにしたいので、 'bool is64 = System.Environment.Is64BitOperatingSystem'というチェックをしますか? –

答えて

1

これを試してみてください:

string Owner = ""; 
string Company = ""; 

OperatingSystem osInfo = System.Environment.OSVersion; 
if (osInfo.Platform == PlatformID.Win32Windows) 
{ 
    // Windows 98             
    Owner = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOwner", "Unknown").ToString(); 
    Company = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOrganization", "Unknown").ToString(); 
} 
else if (osInfo.Platform == PlatformID.Win32NT) 
{ 
    // for NT+     
    RegistryKey localKey; 
    if (Environment.Is64BitOperatingSystem) 
     localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); 
    else 
     localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); 

    Owner = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("RegisteredOwner", "Unknown").ToString(); 
    Company = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("RegisteredOrganization", "Unknown").ToString(); 
} 

lblRegOwner.Text = Owner; 
lblRegOrg.Text = Company; 
+0

これは、トリックを行ったように見えました。以前は所有者/会社をオブジェクトとして使用していて、文字列をキャストしていても何とかGetValueがオーバーロードされていましたが、君は!! –

関連する問題