Setting ComputerName

  • Thread starter Thread starter Todd
  • Start date Start date
T

Todd

Is there a method I can use in C# that will set the ComputerName? Any
Help would be appreciated.

Thanks
Todd
 
This is what I am trying to use:

enum COMPUTER_NAME_FORMAT

{

ComputerNameNetBIOS,

ComputerNameDnsHostname,

ComputerNameDnsDomain,

ComputerNameDnsFullyQualified,

ComputerNamePhysicalNetBIOS,

ComputerNamePhysicalDnsHostname,

ComputerNamePhysicalDnsDomain,

ComputerNamePhysicalDnsFullyQualified,

ComputerNameMax

}

static extern bool SetComputerNameEx( [In] COMPUTER_NAME_FORMAT NameType,

string lpBuffer );

Now this is the part I am not sure of, setting the computer name on a button
click using the value of textbox1.text:

private void button1_Click(object sender, System.EventArgs e)

{

myCName = textBox1.Text;

ComputerName.Form1.SetComputerNameEx(ComputerNameNetBIOS, myCName);

}

Am I even close on this?

Todd
 
Use the Management namespace classes (WMI wrappers) for all this kind of administrative tasks.
Following is a sample (not tested) that renames a local machine when run in the context of an administrator.

using System;
using System.Management;
// Rename a computer account using WMI
public class Wmis {

public static void Main() {

using (ManagementObject dir= new ManagementObject("Win32_ComputerSystem"))
{

ManagementBaseObject inputArgs = dir.GetMethodParameters("CopyEx");
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)
Console.WriteLine("Success");
else Console.WriteLine("Failed with error code: {0}", ret);
}

}
}

Willy.
 
Back
Top