TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Getting MAC Address of Client Machine in C#
MAKAYA Gildas Wilfrid
Sep 01
2014
Code
21.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
//Add the following Using
using
System;
using
System.IO;
// Add a webform in your web app and modify the code for Page_Load event as follows:
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
MyDomainPrefix = @
"CONTOSO\\"
;
//replace CONTOSO with your network domain
string
currentUserLoginName = Request.LogonUserIdentity.Name.Replace(MyDomain,
string
.Empty);
// Get the current Windows authenticated user
string
myCommandToRun =
"/c nbtstat -a "
+ Request.UserHostName + @
" >> "
+ AppDomain.CurrentDomain.BaseDirectory +
currentUserLoginName +
".txt"
;
// Command line to run, I am creating my text file in the web application folder on the server.
Note: nbtstat command does not work on my client machine with Windows 7.
//Execute the command
System.Diagnostics.Process process =
new
System.Diagnostics.Process();
process.StartInfo.CreateNoWindow =
true
;
process.StartInfo.FileName =
"cmd.exe"
;
process.StartInfo.Arguments = myCommandToRun;
process.Start();
process.WaitForExit();
//Start parsing the file created on disk
StreamReader sr =
new
StreamReader(AppDomain.CurrentDomain.BaseDirectory + currentUserLoginName +
".txt"
);
string
MyLine =
null
;
MyLine = sr.ReadToEnd();
string
[] MyStrings = MyLine.Trim().Split(
'\n'
);
//Array of strings from text file split by the return char
foreach
(
string
item
in
MyStrings)
{
if
(item.Trim().Contains(
"MAC Address"
))
{
//Get the MAC Address
Response.Write(item.ToString().Remove(0,item.IndexOf(
'='
)+1).Trim());
}
}
}