2010年4月14日 星期三

修改電腦名稱

using System;
using System.Runtime.InteropServices;
class Program
{
    enum _COMPUTER_NAME_FORMAT
    {
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified,
        ComputerNameMax
    };
    //_COMPUTER_NAME_FORMAT COMPUTER_NAME_FORMAT;

    [DllImport("kernel32.dll")]
    static extern bool SetComputerName(string lpComputerName);

    [DllImport("kernel32.dll")]
    static extern bool SetComputerNameEx(_COMPUTER_NAME_FORMAT iType, string lpComputerName);

    public static void Main()
    {
        string MachineName = "winxp-vm";
        bool succeeded = SetComputerName(MachineName);
        bool succeeded2 = SetComputerNameEx(_COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, MachineName);

        if (succeeded && succeeded2){
            Console.WriteLine("Done! The name will take effect after the next computer restart.");
        }
        else
        {
            Console.WriteLine("Error!");
        }
        Console.ReadKey();
    }
}

沒有留言: