如何阅读';公司名称';从Windows使用C#?

如何阅读';公司名称';从Windows使用C#?,c#,registry,C#,Registry,有些程序读取安装Windows时输入的公司名称,并将其显示在程序中。这是怎么做到的?他们只是从注册表中读取名称吗?检查API系统参数sinfo和一个名为SPI_GETOEMINFO的常量 int details = SystemParametersInfo(SPI_GETOEMINFO, OEMInfo.Capacity, OEMInfo, 0); if (details != 0) { MessageBox.Show(OEMInfo.To

有些程序读取安装Windows时输入的公司名称,并将其显示在程序中。这是怎么做到的?他们只是从注册表中读取名称吗?

检查API系统参数sinfo和一个名为SPI_GETOEMINFO的常量

int details = SystemParametersInfo(SPI_GETOEMINFO, OEMInfo.Capacity, OEMInfo, 0);
        if (details != 0)
        {
            MessageBox.Show(OEMInfo.ToString());
        }
这将返回OEM的公司名称。我认为在安装windows时不必输入公司名称,只需输入计算机名称(这里我可能会出错)

您可以在此处看到所有常量和示例:

如果您想要在注册表中输入的注册公司名称,可以从以下位置获取:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization
使用Registry类,您可以执行以下操作:

string org = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization", "");

Windows将注册的公司名称存储在注册表中,地址为:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization
导入以下内容:

using Microsoft.Win32;
读取所需注册表项的值,如下所示:

RegistryKey hklm = Registry.LocalMachine;
hklm = hklm.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
Object obp = hklm.GetValue("RegisteredOrganization");`
Console.WriteLine("RegisteredOrganization :{0}",obp);`
或者是一个班轮,就像我建议的那样

但是,正确的方法是使用双反斜杠。这是因为反斜杠是C#转义字符-也就是说,您可以使用\n插入换行符,或使用\t插入制表符。因此,为了让C#知道我们想要的是一个普通的反斜杠而不是一些转义字符,我们必须在字符串前面使用两个反斜杠(\)或使用@,如(@“\somestring”):


注意:RegisteredOrganization密钥不保证包含值,因为它可能在操作系统安装期间未被填写。因此,请始终使用try/catch块或检查返回值。

我在.NET中找不到获取信息的方法或属性,但我找到了一个技术说明,告诉您哪个注册表项包含信息:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization

您可以使用WMI阅读此内容,在我看来,您是在寻找Win32_OperatingSystem类,该类的Organization元素包含公司名称

下面的代码是一个显示注册用户和组织的控制台应用程序。要使其运行,您需要在项目中添加对System.Management.dll的引用。应该只有一个管理对象,因此foreach可能是多余的,不太确定这方面的最佳实践是:

using System;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementClass c = new ManagementClass("Win32_OperatingSystem");

            foreach (ManagementObject o in c.GetInstances())
            {
                Console.WriteLine("Registered User: {0}, Organization: {1}", o["RegisteredUser"], o["Organization"]);
            }
            Console.WriteLine("Finis!");
            Console.ReadKey();
        }
    }
}

我不喜欢在我的业务代码中使用text-y注册表调用,我也不喜欢实用程序类,所以我编写了一个扩展方法,从注册表中获取公司名称

using Microsoft.Win32;

namespace Extensions
{
    public static class MyExtensions
    {
        public static string CompanyName(this RegistryKey key)
        {
            // this string goes in my resources file usually
            return (string)key.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("RegisteredOrganization");
        }
    }
}
然后我可以很容易地得到该值:

RegistryKey key = Registry.LocalMachine;
return key.CompanyName();

这没什么特别的,只是一种处理经常检索的注册表值的更漂亮的方法。

System.Environment.CompanyName会很酷的。需要使HKEY\uU。。。使用@或转义反斜杠的逐字字符串文字速度太慢,Russ.)这里的人们看起来目光锐利,很高兴看到这一点。我假设您正在从user32.dll输入系统参数信息。你从哪里得到常数?
RegistryKey key = Registry.LocalMachine;
return key.CompanyName();