2010年7月14日 星期三

自動修改ip,name,workgroup V1.0

在C:\下放置mac.txt檔案 內容為 12345 00:00:00:00:00:00
電腦名稱 :123
電腦編號 :45
網路卡號 :00:00:00:00:00:00
using System;
using System.Management;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

class Program
{
    public static void Main()
    {
        FileInfo filename = new FileInfo("C:\\done");

        if(!filename.Exists)
        {
            string fn = "C:\\mac.txt";
            FileStream fs = new FileStream(fn,FileMode.Open);
            StreamReader sr = new StreamReader(fs,Encoding.UTF8);

            string[] map = new string[50000];
            string mac = APP.GetMAC();

            while(sr.Peek() != -1){
                string tmp = sr.ReadLine();
                string[] tmp_sp = tmp.Split(' ');
                map[Convert.ToInt32(tmp_sp[0])] = tmp_sp[1];
            }
       
            for (int i = 30000; i < 40700; i++)
            {
                if (map[i] != null)
                {
                    if (map[i].CompareTo(mac) == 0)
                    {
                        /*更改電腦名稱,群組,網路位址*/
                   
                        APP.change_name(i);
                        APP.change_group(i);
                        APP.SetNetIP(i);
                        FileStream files = filename.Create();
                        files.Close();                   
                    }
                }
            }
            fs.Close();
        }
    }
}

class APP
{
//修改電腦名稱_code
    enum _COMPUTER_NAME_FORMAT
    {
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified,
        ComputerNameMax
    };
   
    [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 change_name(int i)
    {
        string room = Convert.ToString(i / 100);
        string id = "";
        int sitnum = i % 100;
        if (sitnum < 10)
            id = string.Concat("0",Convert.ToString(sitnum));
        else
            id = Convert.ToString(sitnum);
        string MachineName = "";
        if(room.CompareTo("313") == 0)
            MachineName = string.Concat("P",room,"_",id);
        else
            MachineName = string.Concat("CC",room,"_",id);

        SetComputerName(MachineName);
        SetComputerNameEx(_COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, MachineName);

//修改電腦名稱_code_end

    }

//修改工作群組_code
    public static void change_group(int i)
    {
        string num = Convert.ToString(i / 100);
        string WorkgroupName = "";
        if(num.CompareTo("313") == 0)
            WorkgroupName = string.Concat("P", num);
        else
            WorkgroupName = string.Concat("CC",num);
        ManagementObject manage = new ManagementObject(string.Format("Win32_ComputerSystem.Name='{0}'", Environment.MachineName));
        object[] args = { WorkgroupName, null, null, null };
        manage.InvokeMethod("JoinDomainOrWorkgroup", args);
    }
//修改工作群組_code_end

//取得mac address_code
    public static string GetMAC()
    {
        using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
        {
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"])
                    return mo["MacAddress"].ToString();
            }
        }

        return null;
    }
//取得mac address_code_end

//修改ip address_code
    public static void SetNetIP(int i)
    {
        string class_name =Convert.ToString(i / 100);
        string id = Convert.ToString(i % 100);
        string cclass;
        switch(class_name)
        {
            case "305":
                cclass = "35";
                break;
            case "307":
                cclass = "37";
                break;
            case "402":
                cclass = "42";
                break;
            case "403":
                cclass = "43";
                break;
            case "406":
                cclass = "46";
                break;
            case "313":
                cclass = "13";
                break;
            default:
                cclass = "0";
                break;
        }
        string ip, submask, gateway, DNS1, DNS2;
        string Cls = "Win32_NetworkAdapterConfiguration";
        string NS = "root\\CIMV2";
        ip = string.Concat("192.168.", cclass, ".", id);
        submask = "255.255.255.0";
        gateway = string.Concat("192.168.", cclass, ".254");
        DNS1 = "120.125.1.1";
        DNS2 = "120.125.2.1";
        using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
        {
            string index = "";
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"])
                {
                    index = mo["Index"].ToString();
                }
            }

            ManagementObject objCls = new ManagementObject(NS, Cls + ".INDEX=" + index, null);
            ManagementBaseObject objInPara;

            objInPara = objCls.GetMethodParameters("EnableStatic");
            objInPara["IPAddress"] = new string[] { ip};
            objInPara["SubnetMask"] = new string[] { submask};
            objCls.InvokeMethod("EnableStatic", objInPara, null);

            objInPara = objCls.GetMethodParameters("SetGateways");
            objInPara["DefaultIPGateway"] = new string[] { gateway};
            objCls.InvokeMethod("SetGateways", objInPara, null);

            objInPara = objCls.GetMethodParameters("SetDNSServerSearchOrder");
            objInPara["DNSServerSearchOrder"] = new string[] { DNS1,DNS2};
            objCls.InvokeMethod("SetDNSServerSearchOrder", objInPara, null);

        }
    }
//修改ip address_code_end
}

沒有留言: