Getting IP address & Physical Address from code in c#.

P

Pioneer

Hi,

I need to get MAC address/physical ip address of machine. I am using
below code:


ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
string macAddr = mo["MACAddress"] as string;
if ( macAddr != null && macAddr.Trim() != "" )
return macAddr.ToString();



}


But this returns multiple IP addresses out of which Physical address
is shown twice and rest of the addresses I could not find in
ipconfig/
all command output on cmd.

Could you help me understand the output?


I only want the physical address. IF there is any better alternative,
kindly let me know.


Thanks
Pioneer
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I need to get MAC address/physical ip address of machine. I am using
below code:

ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
string macAddr = mo["MACAddress"] as string;
if ( macAddr != null && macAddr.Trim() != "" )
return macAddr.ToString();

}

But this returns multiple IP addresses out of which Physical address
is shown twice and rest of the addresses I could not find in
ipconfig/
all command output on cmd.

Could you help me understand the output?

I only want the physical address. IF there is any better alternative,
kindly let me know.

Thanks
Pioneer

check for the AdapterType.
Also check the archives, I'm pretty sure this has been answered before
 
I

Ignacio Machin ( .NET/ C# MVP )

I only want the physical address. IF there is any better alternative,
kindly let me know.

ManagementClass objMC = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection colMO = objMC.GetInstances();
string strMACAddress = String.Empty;
foreach (ManagementObject objMO in colMO)
{
    if (strMACAddress == String.Empty)
    {
        if ((bool)objMO["IPEnabled"])
        {
            strMACAddress = objMO["MacAddress"].ToString();
        }
    }
    objMO.Dispose();}

return strMACAddress;

I think that sometimes a virtual device will also support IP, I've in
mind when a 2003 PPC sync with Active Sync the later create a virtual
device with IP 192.168.55.100
 
I

Ignacio Machin ( .NET/ C# MVP )

So what would you suggest...?

To check other properties, like AdapterType. The fact that an adapter
support IP does not garantee that it;s a physical device
 
I

Ignacio Machin ( .NET/ C# MVP )

To check other properties, like AdapterType. The fact that an adapter
support IP does not garantee that it;s a physical device

The correct property is DeviceTypeId
and of course check that the MAC exist , in my computer (XP home) some
of the devices have no MAC address
 
P

Pioneer

Like I said, where are you retrieving a property called DeviceTypeId...?

Hi,
The code worked just fine!! Thanks a lot.

I have one more query. Does MAC address change due to any reason? I
believe as its related to hardware of the PC, it should not/never
change.

Best Regards,
Aditya
 
P

Pioneer

The MAC address can change for any variety of reasons.  Nominally, it's 
embedded in the network adapter, but not all things that look like network  
adapters are actually real hardware, and even hardware network adapaters  
often allow the user to configure the MAC address to be whatever they  
want.  Never mind that any given computer may well have multiple devices  
with MAC addresses, any of which may or may not be visible at any given  
moment (due to being disabled or other).

The fact is, if you think you need the MAC address and you're not writing 
a network analysis program, you're almost certainly wrong about needing  
the MAC address.  It definitely is not a way to uniquely identify the  
computer, in case that's what you're thinking of using it for.

Pete

Thats interesting. I precisely need to make sure that I minimize the
piracy of a program that I am gonna deliver.
Host name, IP address and mac address are the ways I thought of. I
belive in this scenario, does host name fits better?
 
M

Manu

Thats interesting. I precisely need to make sure that I minimize the
piracy of a program that I am gonna deliver.
Host name, IP address and mac address are the ways I thought of. I
belive in this scenario, does host name fits better?

It depends on your policy.
I would never use MAC address or any other hardware/resource id as a
means to tackle software piracy.
Before you think about using Hardware resources for this purpose
Critically examine all the options.
The user of of your application might change its hardware someday (due
to any reason) and having a failed application due to that reason is
really troublesome and irritating.
You can use some way like using some kind of HASP device if you still
want to tie it with hardware. Consider using Aladdin's Products.
 
M

Matthias Krug

Peter said:
I will point out the same thing to you that I point out to anyone else
who brings it up: DRM never stops the people who want to copy your code,
but it often does stop the people who have already paid for a legitimate
license for your code. Anyone who can write code can understand why
those facts mean that DRM is logically the opposite of what a
responsible software developer will do.

100% !!!

We have one CAD software which maybe great for its original purpose, but
is literally unusable - at least if you do care for efficiency.

We paid (and keep paying annual fees) for 9 seats. The license model is
a per seat/per connection model, _NOT_ a named user one.

Every now and then the complete software won't work because of some
minor bug in the DRM modules. Sometimes, because of that, we are losing
half a day for 9 expert(= high salary) CAD workers PLUS 1 from IT dept.

And it is - of course - impossible to hire part time employees, unless
all of them will share indeed the same office with the same machine.

We struggled for 2 years now to achieve an improvement, but there won't
be any.

Consequence: we are direly looking for an alternative and soon will
change the CAD application. That company is losing 9 licenses and an
annual turnover of 18.000 Euro.

Meanwhile, all those who won't care can just go to one of many sites in
the web and get a crack which enables them to use the application
without any payment at all on as many machines as they desire to do.

We did so for testing purposes once. Works like a charm. But paying for
all software we use is not for discussion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top