using System.Management;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml;
protected void Button1_Click(object sender, EventArgs e)
{
//读本机的MAC 地址 ;
string mac = "";
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
//mc = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
this.TextBox1.Text = mac + ":12";
XmlDocument xmlDom = new XmlDocument();
string aaa = HttpContext.Current.Server.MapPath("web.config").ToString();
xmlDom.Load((HttpContext.Current.Server.MapPath("web.config")));
string path = "descendant::configuration/appSettings/add[@key=/"RegisterID/"]";
XmlNode node = xmlDom.SelectSingleNode(path);
if (node != null)
{
node.Attributes["value"].Value = this.TextBox1.Text;
xmlDom.Save((HttpContext.Current.Server.MapPath("web.config")));
}
}
注意:
<!--xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" 有此就不能修改RegisterID的值-->
<configuration>
<appSettings>
<add key="RegisterID" value="00:11:5B:9C:10:5C:12" />
</appSettings>
<configuration>