Setting ComputerName

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Todd

    #1

    Setting ComputerName

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

    Thanks
    Todd
  • Todd Fields

    #2
    Re: Setting ComputerName

    This is what I am trying to use:

    enum COMPUTER_NAME_F ORMAT

    {

    ComputerNameNet BIOS,

    ComputerNameDns Hostname,

    ComputerNameDns Domain,

    ComputerNameDns FullyQualified,

    ComputerNamePhy sicalNetBIOS,

    ComputerNamePhy sicalDnsHostnam e,

    ComputerNamePhy sicalDnsDomain,

    ComputerNamePhy sicalDnsFullyQu alified,

    ComputerNameMax

    }

    static extern bool SetComputerName Ex( [In] COMPUTER_NAME_F ORMAT 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(o bject sender, System.EventArg s e)

    {

    myCName = textBox1.Text;

    ComputerName.Fo rm1.SetComputer NameEx(Computer NameNetBIOS, myCName);

    }

    Am I even close on this?

    Todd

    "Todd" <jjfate@yahoo.c om> wrote in message
    news:f828793f.0 311012218.50a5a b5d@posting.goo gle.com...[color=blue]
    > Is there a method I can use in C# that will set the ComputerName? Any
    > Help would be appreciated.
    >
    > Thanks
    > Todd[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Setting ComputerName

      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.Manageme nt;
      // Rename a computer account using WMI
      public class Wmis {

      public static void Main() {

      using (ManagementObje ct dir= new ManagementObjec t("Win32_Comput erSystem"))
      {

      ManagementBaseO bject inputArgs = dir.GetMethodPa rameters("CopyE x");
      inputArgs["Name"] = "NewComputerNam e";
      // 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.

      ManagementBaseO bject outParams = dir.InvokeMetho d("Rename", inputArgs, null);
      uint ret = (uint)(outParam s.Properties["ReturnValu e"].Value);
      if(ret == 0)
      Console.WriteLi ne("Success");
      else Console.WriteLi ne("Failed with error code: {0}", ret);
      }

      }
      }

      Willy.

      "Todd" <jjfate@yahoo.c om> wrote in message news:f828793f.0 311012218.50a5a b5d@posting.goo gle.com...[color=blue]
      > Is there a method I can use in C# that will set the ComputerName? Any
      > Help would be appreciated.
      >
      > Thanks
      > Todd[/color]


      Comment

      Working...