C# entModel.Win32Exception(); } 返回nameBuilder.ToString(); } } }

C# entModel.Win32Exception(); } 返回nameBuilder.ToString(); } } },c#,.net,windows,computer-name,C#,.net,Windows,Computer Name,您可以使用WMI检索singleton的Name属性。您可以通过为Win32\u ComputerSystem类实例化一个实例并调用它来检索包含唯一实例的数组来完成此操作 使用System.Linq; 使用制度管理; 名称空间SO56585341 { 公共类WmiClassComputerInfoSource:IComputerInfoSource { 公共字符串GetComputerName() { 使用(ManagementClass computerSystemClass=新的管理类(“W

您可以使用WMI检索singleton的
Name
属性。您可以通过为
Win32\u ComputerSystem
类实例化一个实例并调用它来检索包含唯一实例的数组来完成此操作

使用System.Linq;
使用制度管理;
名称空间SO56585341
{
公共类WmiClassComputerInfoSource:IComputerInfoSource
{
公共字符串GetComputerName()
{
使用(ManagementClass computerSystemClass=新的管理类(“Win32_ComputerSystem”))
使用(ManagementObjectCollection computerSystemCollection=computerSystemClass.GetInstances())
使用(ManagementObject computerSystem=computerSystemCollection.Cast().Single())
返回(字符串)计算机系统[“名称”];
}
}
}
…或者通过创建一个并将其用于单独的
Win32\u ComputerSystem
实例

使用System.Linq;
使用制度管理;
名称空间SO56585341
{
公共类WmiSearcherComputerInfoSource:IComputerInfoSource
{
公共字符串GetComputerName()
{
ObjectQuery computerSystemQuery=新建SelectQuery(“Win32_ComputerSystem”);
使用(ManagementObjectSearcher computerSystemSearcher=新的ManagementObjectSearcher(computerSystemQuery))
使用(ManagementObjectCollection computerSystemCollection=computerSystemSearcher.Get())
使用(ManagementObject computerSystem=computerSystemCollection.Cast().Single())
返回(字符串)计算机系统[“名称”];
}
}
}
最后,上面所有方法返回的值似乎最终都存储在注册表中,因此如果您不介意依赖该实现细节,您可以直接从那里检索它

使用Microsoft.Win32;
名称空间SO56585341
{
公共类注册表ComputerInfoSource:IComputerInfoSource
{
公共字符串GetComputerName()
{
//另请参见@“SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\”
// https://www.oreilly.com/library/view/windows-nt-workstation/9781565926134/10_chapter-07.html
常量字符串valueParentKeyPath=@“SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\”;
使用(RegistryKey parentKey=Registry.LocalMachine.OpenSubKey(valueParentKeyPath,false))
返回(字符串)parentKey.GetValue(“ComputerName”);
}
}
}

至于从远程计算机获得相同的值只有上面的最后三种实现可以工作,尽管只需要最少的调整。首先,为了完成这个
IComputerInfoSource
示例,让我们创建一个
abstract
类来保存远程机器名称/地址“参数”

名称空间SO56585341
{
公共抽象类RemoteComputerInfoSource:IComputerInfoSource
{
公共字符串RemoteNameOrIp
{
得到;
}
受保护的RemoteComputerInfoSource(字符串名称ORIP)
{
RemoteNameOrIp=nameOrIp??抛出新的System.ArgumentNullException(nameof(nameOrIp));
}
公共抽象字符串GetComputerName();
}
}
通过
ManagementClass
检索
Win32\u ComputerSystem
实例只需显式地传递一个也指定了

使用System.Linq;
使用制度管理;
名称空间SO56585341
{
公共类RemoteWmiClassComputerInfoSource:RemoteComputerInfoSource
{
公共RemoteWmiClassComputerInfoSource(字符串名称ORIP)
:base(nameOrIp)
{
}
公共重写字符串GetComputerName()
{
ManagementPath computerSystemPath=新的ManagementPath(){
ClassName=“Win32\u计算机系统”,
NamespacePath=@“root\cimv2”,
服务器=RemoteNameOrIp
};
使用(ManagementClass computerSystemClass=新的管理类(computerSystemPath))
使用(ManagementObjectCollection computerSystemCollection=computerSystemClass.GetInstances())
使用(ManagementObject computerSystem=computerSystemCollection.Cast().Single())
返回(字符串)计算机系统[“名称”];
}
}
}
ManagementObjectSearcher
可以通过传递一个类似的
ManagementPath
来使用

使用System.Linq;
使用制度管理;
名称空间SO56585341
{
公共类RemoteWmiSearcherComputerInfoSource:RemoteComputerInfoSource
{
公共RemoteWmiSearcherComputerInfoSource(字符串名称ORIP)
:base(nameOrIp)
{
}
公共重写字符串GetComputerName()
{
ManagementScope computerSystemScope=新的管理范围(
新管理路径(){
NamespacePath=@“root\cimv2”,
服务器=RemoteNameOrIp
}
);
ObjectQuery computerSystemQuery=新建SelectQuery(“Win32_ComputerSystem”);
使用(ManagementObjectSearcher computerSystemSearcher=新的ManagementObjectSearcher(computerSystemScope,computerSystemQuery))
使用(ManagementObjectCollection computerSystemCollection=computerSystemSearcher.Get())
使用(ManagementObject computerSystem=computerSystemCollection.Cast().Single())
返回(字符串)计算机系统[“名称”];
}
}
}
查询远程注册表只需要额外调用以获取远程配置单元根的句柄

使用Microsoft.Win32;
名称空间SO56585341
{
公共类RemoteRegistryComputerInfoSource:RemoteComputerInfoSource
{
公共Re
IPHostEntry hostEntry = Dns.GetHostEntry(_ip);

_hostname = hostEntry.HostName;
        //declare a string to be our machinename
        string machineName;
        //declare a string which we will pass into powershell later as script
        //assigns the hostname or IP
        string getComputer = "$ip = " + "\"" + ip + "\"" + "\r\n";
        //add to the string this, which gets the Win32_ComputerSystem.. @BACON knew what I was after
        //we pipe that back using |select -expand Name
        getComputer += "get-wmiobject -class Win32_ComputerSystem -property Name -ComputerName " + "$ip " +
            "|select -expand Name";
        //create a powershell instance using
        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            //add the script into our instance of ps
            PowerShellInstance.AddScript(getComputer);
            //instantiate a collection to house our output from PS
            //you could also probably just instantiate a PSObject instead of a collection.. but this might be useful if modified to get an array of computer names... and this is how I did it so can't verify
            Collection<PSObject> psOutput;
            //assign psOutput from .Invoke() method
            psOutput = PowerShellInstance.Invoke();

            //you could trim this loop and get rid of it for only one IP
            foreach (var item in psOutput)
            {
               //machineName = MachineName||ComputerName string NOT hostname
                machineName = item.BaseObject.ToString();
            }
        }