我现在要自己根据输入的信息来设置计算机的机器名称和工作组,我是通过注册表还是可以直接有API来设置?
请给我个说明,谢谢!

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    //查询网络上的计算机IP和用户需要引用
    using System.Data;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    //关键引用空间System.directoryservices.dll,System.Management.dll
    using System.DirectoryServices;
    using System.Management;
    //获取网络邻居中的所有计算机IP和当前登录用户   
       string strDomain;
       string strComputer;
       string strShare;   
       
       DirectoryEntry root =new DirectoryEntry("WinNT:");
       foreach(DirectoryEntry Domain in root.Children)
       {
       //枚举工作组或域 
        strDomain=Domain.Name;
        comboBox1.Items.Add(strDomain);
        foreach(DirectoryEntry Computer in Domain.Children)
        {
        //枚举指定工作组或域的计算机
         
          if(Computer.SchemaClassName.Equals("Computer"))
          {
           strComputer=Computer.Name;
           comboBox2.Items.Add(strComputer);      
           
          
          //枚举指定计算机的共享文件夹 
          
           //获取本机共享的文件夹
           ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share");
            foreach (ManagementObject share in searcher.Get()) 
           {
            strShare=share["Name"].ToString();
            comboBox3.Items.Add(strShare);         
           }     
          
          }    
        }  
       }   
      

  2.   

    声明:   
        //api   SetComputerNameEx   
        //iType說明:   
        //typedef   enum   _COMPUTER_NAME_FORMAT     
        //   {   
        //   ComputerNameNetBIOS,   
        //   ComputerNameDnsHostname,   
        //   ComputerNameDnsDomain,   
        //   ComputerNameDnsFullyQualified,   
        //   ComputerNamePhysicalNetBIOS,   
        //   ComputerNamePhysicalDnsHostname,   
        //   ComputerNamePhysicalDnsDomain,   
        //   ComputerNamePhysicalDnsFullyQualified,   
        //   ComputerNameMax   
        //   }   COMPUTER_NAME_FORMAT   ;   
        //   
        //lpComputerName說明:   計算機名稱   
        [DllImport(“kernel32.dll“,EntryPoint=“SetComputerNameEx“)]   public   static   extern   int   apiSetComputerNameEx(int   iType,string   lpComputerName);   
           
        设定计算机名称:   
        //   set   computer   Name   
        int   i   =   apiSetComputerNameEx(5,”RobertTest!”);   
        if   (i   ==   0)   
        {   
           MessageBox.Show(“Modify   Computer   Name   failed,Please   try   again!“);   
        }   
        else   
        {   
           MessageBox.Show(“Computer   Name   is   Modified,   Please   Restart   Computer   Now!“);   
        }   
           
      

  3.   

    API和注册表都可以 .... 但都需要重新启动生效的[DllImport("kernel32.dll", EntryPoint="SetComputerName")]
    public static extern int SetComputerName(string lpComputerName);