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.
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.