Guardar la MAC en una variable.

(1/2) > >>

Meta:
Hola:

Me falla este programa, quiero coger la MAC de este formato que genera en la primera tarjeta de red física (04-B2-AF-EE-E2-34-6A-8C).

Al complicar, me dale este error.
Código
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.Net.NetworkInformation; // No olvidar.
 
namespace Leer_MAC
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.Title = "En busca del MAC";
 
           string Nombre_HostName = null;
           string Nombre_Dominio = null;
           string MAC = null;
 
           IPGlobalProperties Propiedades_PC = IPGlobalProperties.GetIPGlobalProperties();
           NetworkInterface[] Interfaz_red = NetworkInterface.GetAllNetworkInterfaces();
           Console.WriteLine("Información de interfaz para {0}.{1}     ",
                   Propiedades_PC.HostName, Propiedades_PC.DomainName);
           if ((Interfaz_red == null) || (Interfaz_red.Length < 1))
           {
               Console.WriteLine("  No hay interfaces de red encontrados.");
               return;
           }
 
           Console.WriteLine("  Número de interfaces .................... : {0}", Interfaz_red.Length);
           foreach (NetworkInterface Adaptador in Interfaz_red)
           {
               IPInterfaceProperties Propiedades = Adaptador.GetIPProperties(); //  .GetIPInterfaceProperties();
               Console.WriteLine();
               Console.WriteLine(Adaptador.Description);
               Console.WriteLine(String.Empty.PadLeft(Adaptador.Description.Length, '='));
               Console.WriteLine("  Tipo interfaz ........................... : {0}", Adaptador.NetworkInterfaceType);
               Console.Write("  Dirección física ........................ : ");
               PhysicalAddress Direccion = Adaptador.GetPhysicalAddress();
               byte[] bytes = Direccion.GetAddressBytes();
               for (int i = 0; i < bytes.Length; i++)
               {
                   // Muestra la dirección física en hexadecimal.
                   Console.Write("{0}", bytes[i].ToString("X2"));
                   // Inserte un guión después de cada bocado, a menos que estemos al final de la dirección.
                   if (i != bytes.Length - 1)
                   {
                       Console.Write("-");
                   }
               }
               Console.WriteLine();
           }
 
           // Guarda el nombre del hostname en la variable Nombre_HostName.
           Nombre_HostName = Propiedades_PC.HostName;
 
           // Guarda el nombre del dominio si lo tiene.
           Nombre_Dominio = Propiedades_PC.DomainName;
 
 
           // Guarda la MAC recibida con sus - en la varible MAC.
           MAC = Encoding.UTF8.GetString(bytes);
 
           Console.WriteLine();
           Console.WriteLine(@"Nombre del HostName: {0}", Nombre_HostName);
           Console.WriteLine(@"Nombre del domninio: {0}", Nombre_Dominio);
           Console.WriteLine(@"MAC es: {0}", MAC);
           Console.ReadKey(); // Pulsa cualquier tecla y sale.
       }
   }
}
 

Citar

Gravedad   Código   Descripción   Proyecto   Archivo   Línea
Error   CS0103   El nombre 'bytes' no existe en el contexto actual   Leer_MAC   C:\Users\Usuario\Documents\Visual Studio 2015\Projects\Leer_MAC\Leer_MAC\Program.cs   65


Buscando el cambio de tipo de Byte[] a string no me ha funcionado. En esta web ayuda como se hace.
http://www.convertdatatypes.com/Convert-Byte-Array-to-string-in-CSharp.html

¿Alguna idea?

Saludos.

ivancea96:
Estás utilizando la variable bytes fuera del bucle. Está declarada dentro del contexto del bucle foreach.

Meta:
Hola:

Código
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.Net.NetworkInformation; // No olvidar.
 
namespace Leer_MAC
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.Title = "En busca del MAC";
 
           string Nombre_HostName = null;
           string Nombre_Dominio = null;
           string MAC = null;
 
           IPGlobalProperties Propiedades_PC = IPGlobalProperties.GetIPGlobalProperties();
           NetworkInterface[] Interfaz_red = NetworkInterface.GetAllNetworkInterfaces();
           Console.WriteLine("Información de interfaz para {0}.{1}     ",
                   Propiedades_PC.HostName, Propiedades_PC.DomainName);
           if ((Interfaz_red == null) || (Interfaz_red.Length < 1))
           {
               Console.WriteLine("  No hay interfaces de red encontrados.");
               return;
           }
 
           Console.WriteLine("  Número de interfaces .................... : {0}", Interfaz_red.Length);
           foreach (NetworkInterface Adaptador in Interfaz_red)
           {
               IPInterfaceProperties Propiedades = Adaptador.GetIPProperties(); //  .GetIPInterfaceProperties();
               Console.WriteLine();
               Console.WriteLine(Adaptador.Description);
               Console.WriteLine(String.Empty.PadLeft(Adaptador.Description.Length, '='));
               Console.WriteLine("  Tipo interfaz ........................... : {0}", Adaptador.NetworkInterfaceType);
               Console.Write("  Dirección física ........................ : ");
               PhysicalAddress Direccion = Adaptador.GetPhysicalAddress();
               byte[] bytes = Direccion.GetAddressBytes();
               // Variable que tendra la dirección visible
               string direccion_MAC = string.Empty;
               // Recorrer todos los bytes de la dirección.
               for (int i = 0; i < bytes.Length; i++)
               {
                   // Muestra la dirección física en hexadecimal.
                   direccion_MAC += bytes[i].ToString("X2");
                   Console.Write("{0}", bytes[i].ToString("X2"));
                   // Inserte un guión después de cada bocado, a menos que estemos al final de la dirección.
                   if (i != bytes.Length - 1)
                   {
                       // Agregar un separador, por formato.
                       direccion_MAC += "-";
                       Console.Write("-");
                   }
                   MAC = direccion_MAC;
               }
               Console.WriteLine();
           }
 
           // Guarda el nombre del hostname en la variable Nombre_HostName.
           Nombre_HostName = Propiedades_PC.HostName;
 
           // Guarda el nombre del dominio si lo tiene.
           Nombre_Dominio = Propiedades_PC.DomainName;
 
 
           // Guarda la MAC recibida con sus - en la varible MAC.
           //MAC = direccion_MAC;
 
           Console.WriteLine();
           Console.WriteLine(@"Nombre del HostName: {0}", Nombre_HostName);
           Console.WriteLine(@"Nombre del domninio: {0}", Nombre_Dominio);
           Console.WriteLine(@"MAC es: {0}", MAC);
           Console.ReadKey(); // Pulsa cualquier tecla y sale.
       }
   }
}

¿Hay alguna forma de tener ese MAC y guardarla en una variable general tipo string?

Me detecta todos los MAC tanto físico y virtuales. Solo me interesa el físico. Eso si, he logrado sacar la MAC pero solo me muestra el último llamado Tunnel.

Mira que llevo tiempo con esto y no me sale ni aquí ni pekín.

Saludos.

crack81:
Hola no se muy bien cual es tu  falla pero mira este codigo
para optimizar un poco y ver mas facil lo que andas buscado

Código
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation; // No olvidar.
 
namespace Leer_MAC
{
class Program
{
static void Main(string[] args)
{
Console.Title = "En busca del MAC";
 
var macAddr =
(
from nic in NetworkInterface.GetAllNetworkInterfaces()
select nic.GetPhysicalAddress().ToString()
).ToArray();
 
Console.WriteLine("numero de direcciones "+macAddr.Count());
 
foreach(var address in macAddr){
Console.WriteLine(address);
}
 
Console.ReadLine();
 
}
}
}

kondrag_X1:
Hola Crack81,

Esto es link??? Que hace falta para incluirlo alguna libreria;
Código
var macAddr =
(
from nic in NetworkInterface.GetAllNetworkInterfaces()
select nic.GetPhysicalAddress().ToString()
).ToArray();
 

Navegación

[0] Índice de Mensajes

[#] Página Siguiente