How to change my PC name?

  • Thread starter Thread starter Billy Bob
  • Start date Start date
B

Billy Bob

Hello

I'm looking for a way to change my system name via c#. I know I'll need to
use WMI, anyone have any examples?
WMI has never been fun for me to use.
 
Billy Bob said:
Hello

I'm looking for a way to change my system name via c#. I know I'll need to use WMI,
anyone have any examples?
WMI has never been fun for me to use.


If WMI is no fun, you can always use the command line utility net.exe
All you have to do is shell-out the following commands using Process.Start.

net computer \\actualname /del
followed by ...
net computer \\newname /add

and you are done.

Willy.
 
Thanks for that idea, but I'd rather do it C# so anyone that has to maintain
my code in the future wount have to bother with that.

I've got ...

ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();

According to MSDN http://msdn2.microsoft.com/en-us/library/aa393056.aspx
there is a rename method but I'm not sure how to get to it.
 
Billy Bob said:
Thanks for that idea, but I'd rather do it C# so anyone that has to maintain my code in
the future wount have to bother with that.

I've got ...

ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();

According to MSDN http://msdn2.microsoft.com/en-us/library/aa393056.aspx there is a rename
method but I'm not sure how to get to it.

But you can do it in C#, Start is a method of the System.Diagnostics namespace class
Process, all you need is a few lines of code using this.
But if you want to go WMI you need to call the Rename function, here is something that might
get you started.

using (ManagementObject dir= new ManagementObject("Win32_ComputerSystem"))
{
ManagementBaseObject inputArgs = dir.GetMethodParameters("Rename");
inputArgs["Name"] = "newComputerName";
// Other input args are UserName and Password.
// If not specified the current user security context is used to execute the command.
// See WMI sdk docs or MSDN for details.

ManagementBaseObject outParams = dir.InvokeMethod("Rename", inputArgs, null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
...// success
else
..

Willy.
 
I get a ArgumentOutOfRangeException when the debugger excutes this line...
using (ManagementObject dir= new ManagementObject("Win32_ComputerSystem"))

Any Ideas?


Willy Denoyette said:
Billy Bob said:
Thanks for that idea, but I'd rather do it C# so anyone that has to
maintain my code in the future wount have to bother with that.

I've got ...

ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();

According to MSDN http://msdn2.microsoft.com/en-us/library/aa393056.aspx
there is a rename method but I'm not sure how to get to it.

But you can do it in C#, Start is a method of the System.Diagnostics
namespace class Process, all you need is a few lines of code using this.
But if you want to go WMI you need to call the Rename function, here is
something that might get you started.

using (ManagementObject dir= new ManagementObject("Win32_ComputerSystem"))
{
ManagementBaseObject inputArgs = dir.GetMethodParameters("Rename");
inputArgs["Name"] = "newComputerName";
// Other input args are UserName and Password.
// If not specified the current user security context is used to execute
the command.
// See WMI sdk docs or MSDN for details.

ManagementBaseObject outParams = dir.InvokeMethod("Rename", inputArgs,
null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
...// success
else
..

Willy.
 
Billy Bob said:
I get a ArgumentOutOfRangeException when the debugger excutes this line...
using (ManagementObject dir= new ManagementObject("Win32_ComputerSystem"))

Any Ideas?


Yep, what I've posted was not a complete sample :-(

You have to supply the current computer name, say you get it from the environment.

string compPath= "Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'";
using (ManagementObject comp = new ManagementObject(new ManagementPath(compPath)))
{
ManagementBaseObject inputArgs = comp.GetMethodParameters("Rename");
...

Willy.
 
Thank You for your help, that works for me.
Willy Denoyette said:
Yep, what I've posted was not a complete sample :-(

You have to supply the current computer name, say you get it from the
environment.

string compPath= "Win32_ComputerSystem.Name='" +
System.Environment.MachineName + "'";
using (ManagementObject comp = new ManagementObject(new
ManagementPath(compPath)))
{
ManagementBaseObject inputArgs = comp.GetMethodParameters("Rename");
..

Willy.
 
Back
Top