Reputation: 6996
I have built a small Windows application which finds the MAC address of the computer. I also have an ASP.NET webpage. When my Login page loads, I am running that executable.
I am trying to get MAC address value. How can I achieve this?
Can my desktop application return that value to my web page?
Here is what I have tried so far.
Desktop application code:
public string GetSystemMACID()
{
string systemName = System.Windows.Forms.SystemInformation.ComputerName;
try
{
ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollectionOfResults = theSearcher.Get();
foreach (ManagementObject theCurrentObject in theCollectionOfResults)
{
if (theCurrentObject["MACAddress"] != null)
{
string macAdd = theCurrentObject["MACAddress"].ToString();
return macAdd.Replace(':', '-');
}
}
}
catch (ManagementException e)
{
}
catch (System.UnauthorizedAccessException e)
{
}
return string.Empty;
}
The return value is just assigned to a Label
.
Can anyone suggest me if it is possible at all? Any suggestions are welcome.
Upvotes: 1
Views: 1223
Reputation: 1537
You could set up your site to accept a query parameter that is called MACAddress. Have the desktop app POST to the website; POSTing the value of the cookie. This may help:
using System.Net;
...
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.UseDefaultCredentials = true;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
byte[] requestBytes = Encoding.UTF8.GetBytes(queryString);
httpWebRequest.ContentLength = requestBytes.Length;
using (var requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
The query string will look like
"MACAddress=" + macAdd
-------------update upon request---------------
Within your Desktop Application, add the using statement. You may need to add the reference to the Dll within solution explorer too.
Then, make a method called PostMacAddress as follows:
public void PostMacAddress(string url, string macAdd)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.UseDefaultCredentials = true;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
var queryString = "MACAddress=" + macAdd;
byte[] requestBytes = Encoding.UTF8.GetBytes(queryString);
httpWebRequest.ContentLength = requestBytes.Length;
using (var requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
}
I'm not sure what it is you're not understanding (not trying to be mean). I'm simplifying here, but, POSTing is an HTTP protocol for sending data to a website. Another one is GET (protocol for reading data).
Hope that helps!
-------updating to show the web side...-----------
In your Page_Load method, you'll want to get the QueryString like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Request.QueryString["MACAddress"])
lblMacAddress.Text = Request.QueryString["MACAddress"];
}
Upvotes: 2