以编程方式重命名计算机c#.net我需要通过.net应用程序重命名我的计算机。我试过这段代码:publicstaticboolSetMachineName(stringnewName){MessageBox.Show(String.Format("SettingMachineNameto'{0}'...",newName));//调用WMI以使用(ManagementObjectwmiObject=newManagementObject(newManagementPath(String.Format("Win32_ComputerSystem.Name='{0}'",System.Environment.MachineName)))){ManagementBaseObjectinputArgs=wmiObject.GetMethodParameters("重命名");inputArgs["姓名"]=newName;//设置名称ManagementBaseObjectoutParams=wmiObject.InvokeMethod("Rename",inputArgs,null);uintret=(uint)(outParams.Properties["ReturnValue"].Value);if(ret==0){//有效returntrue;}else{//没有成功returnfalse;}}}但它不起作用。我试过这个:使用System.Runtime.InteropServices;[DllImport("kernel32.dll")]staticexternboolSetComputerName(stringlpComputerName);publicstaticboolSetMachineName(stringnewName){booldone=SetComputerName(newName);如果(完成){{MessageBox.Show("完成");返回真;}}else{MessageBox.Show("失败");返回假;但它也没有用。我已经尝试了我发现的所有更改计算机名称的方法,但没有人在工作......它不会更改计算机名称......唯一有效的方法是当我查找一些注册表项时,这个是代码,就是这样吗?publicstaticboolSetMachineName(stringnewName){RegistryKeykey=Registry.LocalMachine;stringactiveComputerName="SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName";RegistryKeyactiveCmpName=key.CreateSubKey(activeComputerName);activeCmpName.SetNameValue("ComputerName",new);activeCmpName.Close();stringcomputerName="SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName";RegistryKeycmpName=key.CreateSubKey(computerName);cmpName.SetValue("ComputerName",newName);cmpName.Close();string_hostName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\";RegistryKeyhostName=key.CreateSubKey(_hostName);hostName.SetValue("主机名",newName);hostName.SetValue("NV主机名",newName);主机名.Close();返回真;}并在重新启动后名称更改....从MSDN文档中获取SetComputerName..为本地计算机设置新的NetBIOS名称。名称存储在注册表中,名称更改在用户下次重新启动计算机时生效。您是否尝试过重新启动计算机?WMI对象设置计算机名称。然后使用注册表检查名称是否已设置。因为System.Environment.MachineName不会立即更新。CMD.exe中的“主机名”命令仍然输出旧名称。所以还是需要重启。但是注册表检查可以查看名称是否已设置。希望这可以帮助。BooleanSetComputerName(StringName){StringRegLocComputerName=@"SYSTEMCurrentControlSetControlComputerNameComputerName";尝试{stringcompPath="Win32_ComputerSystem.Name='"+System.Environment.MachineName+"'";使用(ManagementObjectmo=newManagementObject(newManagementPath(compPath))){ManagementBaseObjectinputArgs=mo.GetMethodParameters("重命名");inputArgs["姓名"]=姓名;ManagementBaseObjectoutput=mo.InvokeMethod("重命名",inputArgs,null);uintretValue=(uint)Convert.ChangeType(output.Properties["ReturnValue"].Value,typeof(uint));if(retValue!=0){thrownewException("由于未知原因无法更改计算机。");}}RegistryKeyComputerName=Registry.LocalMachine.OpenSubKey(RegLocComputerName);if(ComputerName==null){thrownewException("注册表位置'"+RegLocComputerName+"'不可读。");}if(((String)ComputerName.GetValue("ComputerName"))!=Name){thrownewException("Thecomputer名称由WMI设置但未在注册表位置更新:'"+RegLocComputerName+"'");}ComputerName.Close();ComputerName.Dispose();}catch(Exceptionex){returnfalse;}returntrue;}ProgrammaticallyRenamingComputersUsingC#这是一篇很长的帖子,我不确定到底有什么直接关系,所以我不会贴一个片段以上是C#学习教程:ProgrammaticallyRenamingComputersc分享的所有内容#.net,如果对你有用,需要了解更多C#学习教程,希望大家多多关注。本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如需转载请注明出处:
