From 734396e7a82dec0ee05131504510ca148588120e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 8 Jun 2013 13:03:09 -0400 Subject: [PATCH] get mac address in a mono compatible way --- .../NetworkManagement/NetworkManager.cs | 101 ++---------------- 1 file changed, 6 insertions(+), 95 deletions(-) diff --git a/MediaBrowser.Common.Implementations/NetworkManagement/NetworkManager.cs b/MediaBrowser.Common.Implementations/NetworkManagement/NetworkManager.cs index 6db00064cf..6d35d5f861 100644 --- a/MediaBrowser.Common.Implementations/NetworkManagement/NetworkManager.cs +++ b/MediaBrowser.Common.Implementations/NetworkManagement/NetworkManager.cs @@ -2,11 +2,10 @@ using MediaBrowser.Model.Net; using System; using System.Collections.Generic; -using System.Diagnostics; using System.Globalization; using System.Linq; -using System.Management; using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.InteropServices; @@ -46,104 +45,16 @@ namespace MediaBrowser.Common.Implementations.NetworkManagement return port; } - /// - /// Creates the netsh URL registration. - /// - public void AuthorizeHttpListening(string url) - { - var startInfo = new ProcessStartInfo - { - FileName = "netsh", - Arguments = string.Format("http add urlacl url={0} user=\"NT AUTHORITY\\Authenticated Users\"", url), - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - Verb = "runas", - ErrorDialog = false - }; - - using (var process = Process.Start(startInfo)) - { - process.WaitForExit(); - } - } - - /// - /// Adds the windows firewall rule. - /// - /// The port. - /// The protocol. - public void AddSystemFirewallRule(int port, NetworkProtocol protocol) - { - // First try to remove it so we don't end up creating duplicates - RemoveSystemFirewallRule(port, protocol); - - var args = string.Format("advfirewall firewall add rule name=\"Port {0}\" dir=in action=allow protocol={1} localport={0}", port, protocol); - - RunNetsh(args); - } - - /// - /// Removes the windows firewall rule. - /// - /// The port. - /// The protocol. - public void RemoveSystemFirewallRule(int port, NetworkProtocol protocol) - { - var args = string.Format("advfirewall firewall delete rule name=\"Port {0}\" protocol={1} localport={0}", port, protocol); - - RunNetsh(args); - } - - /// - /// Runs the netsh. - /// - /// The args. - private void RunNetsh(string args) - { - var startInfo = new ProcessStartInfo - { - FileName = "netsh", - Arguments = args, - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - Verb = "runas", - ErrorDialog = false - }; - - using (var process = new Process { StartInfo = startInfo }) - { - process.Start(); - process.WaitForExit(); - } - } - /// /// Returns MAC Address from first Network Card in Computer /// /// [string] MAC Address public string GetMacAddress() { - var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); - var moc = mc.GetInstances(); - var macAddress = String.Empty; - foreach (ManagementObject mo in moc) - { - if (macAddress == String.Empty) // only return MAC Address from first card - { - try - { - if ((bool)mo["IPEnabled"]) macAddress = mo["MacAddress"].ToString(); - } - catch - { - mo.Dispose(); - return ""; - } - } - mo.Dispose(); - } - - return macAddress.Replace(":", ""); + return NetworkInterface.GetAllNetworkInterfaces() + .Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback) + .Select(i => BitConverter.ToString(i.GetPhysicalAddress().GetAddressBytes())) + .FirstOrDefault(); } /// @@ -339,7 +250,7 @@ namespace MediaBrowser.Common.Implementations.NetworkManagement } protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - + /// /// Gets the port. ///