ASP.NET: Get MAC Address of Adapter, and Control.Invoke
using System;
using System.Net.NetworkInformation;
using System.Web;
namespace MACAddress
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Response.Write("Interface information for "+ computerProperties.HostName + ": "+ computerProperties.DomainName + "<BR/>");
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Response.Write(adapter.Description + "<BR/>");
Response.Write(" Interface type .......................... : "+ adapter.NetworkInterfaceType.ToString() +"<BR/>");
Response.Write(" Physical Address ........................ :"+ adapter.GetPhysicalAddress().ToString() +"<BR/>");
Response.Write("<HR/>");
}
}
}
}
Control.Invoke
Visual Studio 2005 developers have learned that 2005 is a lot more picky about attempts to update a control property in a Form from anything other than the UI thread the control was created on. Here is an elegant way to do this that is very adaptable and re-usable:
delegate void SetValueDelegate(Object obj, Object val, Object[] index);
public void SetControlProperty(Control ctrl, String propName, Object val)
{
PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
Delegate dgtSetValue = new SetValueDelegate(propInfo.SetValue);
ctrl.Invoke(dgtSetValue, new Object[3] { ctrl, val, /*index*/ null });
}
// Usage: SetControlProperty(label1, "Text", myString);
That's it! Works with any control, all you need to do is get the control name, the property name (in "quotes") and the value correct.
Comments
Post a Comment