Rename my Computer

G

Guest

HI,

I need to install a service that changes Host Name, computer name, on all
our machines.
I have tried to ways:
First one:
static void RenameHostName()
{
ManagementClass mComputerSystem = new ManagementClass
("Win32_ComputerSystem");
ManagementBaseObject objNewComputerName =

CoomputerSystem.GetMethodParameters("Rename");
Console.WriteLine(objNewComputerName["Name"]);
objNewComputerName["Name"] = "NewHostName";
Console.WriteLine(objNewComputerName["Name"]);
mComputerSystem.InvokeMethod("Rename",objNewComputerName,null);
}

Second way
static void RenameHostName2()
{
using(ManagementObject cs = new
ManagementObject(@"Win32_Computersystem.Name='myHostName'"))

{

cs.Get();

ManagementBaseObject inParams = cs.GetMethodParameters("Rename");

inParams["Name"] = "NewHostName";

ManagementBaseObject outParams = cs.InvokeMethod("Rename", inParams,
null);

// Check return value ...

uint ret = (uint)outParams.Properties["ReturnValue"].Value;

}
}

None of these has worked, my hostName is still the same..... Do I have to
restart my computer after changing hostName or will the the system change
without restarting my computer???

is there any other way to change host name??? All new Ideas are very
appreciated.
 
G

Guest

I know that if you change the computer name manually, you have to
restart the computer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top