renommer l’ordinateur par programme c # .net

Je dois renommer mon ordinateur via une application .net. J’ai essayé ce code:

public static bool SetMachineName(ssortingng newName) { MessageBox.Show(Ssortingng.Format("Setting Machine Name to '{0}'...", newName)); // Invoke WMI to populate the machine name using (ManagementObject wmiObject = new ManagementObject(new ManagementPath(Ssortingng.Format("Win32_ComputerSystem.Name='{0}'",System.Environment.MachineName)))) { ManagementBaseObject inputArgs = wmiObject.GetMethodParameters("Rename"); inputArgs["Name"] = newName; // Set the name ManagementBaseObject outParams = wmiObject.InvokeMethod("Rename",inputArgs,null); uint ret = (uint)(outParams.Properties["ReturnValue"].Value); if (ret == 0) { //worked return true; } else { //didn't work return false; } } } 

mais ça n’a pas marché.

et j’ai essayé celui-ci:

 using System.Runtime.InteropServices; [DllImport("kernel32.dll")] static extern bool SetComputerName(ssortingng lpComputerName); public static bool SetMachineName(ssortingng newName) { bool done = SetComputerName(newName); if (done) { { MessageBox.Show("Done"); return true; } } else { MessageBox.Show("Failed"); return false; } } 

mais cela n’a pas fonctionné non plus.

J’ai essayé tous les moyens que j’ai trouvés pour changer le nom de l’ordinateur et personne ne travaille ….. il ne change pas le nom de l’ordinateur … la seule façon dont cela a fonctionné est quand j’ai chaged certaines valeurs de clé de registre, c’est le code, est-ce que ça va?

 public static bool SetMachineName(ssortingng newName) { RegistryKey key = Registry.LocalMachine; ssortingng activeComputerName = "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"; RegistryKey activeCmpName = key.CreateSubKey(activeComputerName); activeCmpName.SetValue("ComputerName", newName); activeCmpName.Close(); ssortingng computerName = "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName"; RegistryKey cmpName = key.CreateSubKey(computerName); cmpName.SetValue("ComputerName", newName); cmpName.Close(); ssortingng _hostName = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\"; RegistryKey hostName = key.CreateSubKey(_hostName); hostName.SetValue("Hostname",newName); hostName.SetValue("NV Hostname",newName); hostName.Close(); return true; } 

et après le redémarrage, le nom change …

De la documentation MSDN de SetComputerName ..

Définit un nouveau nom NetBIOS pour l’ordinateur local. Le nom est stocké dans le registre et le changement de nom prend effet au prochain redémarrage de l’utilisateur.

Avez-vous essayé de redémarrer l’ordinateur?

Un object WMI définit le nom de l’ordinateur. Ensuite, le registre est utilisé pour vérifier si le nom a été défini. Parce que System.Environment.MachineName n’est pas mis à jour immédiatement. Et la commande ‘hostname’ dans CMD.exe génère toujours l’ancien nom. Donc, un redémarrage est toujours nécessaire. Mais avec la vérification de registre peut voir si le nom a été défini.

J’espère que cela t’aides.

 Boolean SetComputerName(Ssortingng Name) { Ssortingng RegLocComputerName = @"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"; try { ssortingng compPath= "Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'"; using (ManagementObject mo = new ManagementObject(new ManagementPath(compPath))) { ManagementBaseObject inputArgs = mo.GetMethodParameters("Rename"); inputArgs["Name"] = Name; ManagementBaseObject output = mo.InvokeMethod("Rename", inputArgs, null); uint retValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint)); if (retValue != 0) { throw new Exception("Computer could not be changed due to unknown reason."); } } RegistryKey ComputerName = Registry.LocalMachine.OpenSubKey(RegLocComputerName); if (ComputerName == null) { throw new Exception("Registry location '" + RegLocComputerName + "' is not readable."); } if (((Ssortingng)ComputerName.GetValue("ComputerName")) != Name) { throw new Exception("The computer name was set by WMI but was not updated in the registry location: '" + RegLocComputerName + "'"); } ComputerName.Close(); ComputerName.Dispose(); } catch (Exception ex) { return false; } return true; } 

Renommer par programme un ordinateur en utilisant C #

C’est un long article et je ne sais pas exactement ce qui sera directement pertinent, je ne collerai donc pas d’extrait