private void mpButton1_Click(object sender, EventArgs e) { String macAddress; byte[] hwAddress; string hostName = ""; WakeOnLanManager wakeOnLanManager = new WakeOnLanManager(); IPAddress ipAddress = null; string detectedFolderName = ""; if (!Util.Utils.IsUNCNetwork(folderTextBox.Text)) { // Check if letter drive is a network drive detectedFolderName = Util.Utils.FindUNCPaths(folderTextBox.Text); } if (Util.Utils.IsUNCNetwork(detectedFolderName)) { hostName = Util.Utils.GetServerNameFromUNCPath(detectedFolderName); } else if (Util.Utils.IsUNCNetwork(folderTextBox.Text)) { hostName = Util.Utils.GetServerNameFromUNCPath(folderTextBox.Text); } if (string.IsNullOrEmpty(hostName)) { MessageBox.Show("Wrong unc path " + folderTextBox.Text, "MediaPortal Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Log.Debug("Wrong unc path {0}", folderTextBox.Text); return; } using (Profile.Settings xmlreader = new MPSettings()) { macAddress = xmlreader.GetValueAsString("macAddress", hostName, null); } // Check if we already have a valid IP address stored, // otherwise try to resolve the IP address if (!IPAddress.TryParse(hostName, out ipAddress)) { // Get IP address of the server try { IPAddress[] ips; ips = Dns.GetHostAddresses(hostName); Log.Debug("WakeUpServer: WOL - GetHostAddresses({0}) returns:", hostName); foreach (IPAddress ip in ips) { Log.Debug(" {0}", ip); ipAddress = ip; // Check for valid IP address if (ipAddress != null) { // Update the MAC address if possible hwAddress = wakeOnLanManager.GetHardwareAddress(ipAddress); if (wakeOnLanManager.IsValidEthernetAddress(hwAddress)) { Log.Debug("WakeUpServer: WOL - Valid auto MAC address: {0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}" , hwAddress[0], hwAddress[1], hwAddress[2], hwAddress[3], hwAddress[4], hwAddress[5]); // Store MAC address macAddress = BitConverter.ToString(hwAddress).Replace("-", ":"); Log.Debug("WakeUpServer: WOL - Store MAC address: {0}", macAddress); using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.MPSettings()) { xmlwriter.SetValue("macAddress", hostName, macAddress); } MessageBox.Show("Stored MAC address: " + macAddress, "MediaPortal Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("WakeUpServer: WOL - Not a valid IPv4 address: " + ipAddress, "MediaPortal Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Log.Debug("WakeUpServer: WOL - Not a valid IPv4 address: {0}", ipAddress); } } } } catch (Exception ex) { Log.Error("WakeUpServer: WOL - Failed GetHostAddress - {0}", ex.Message); } } }
public static bool HandleWakeUpServer(string hostName, int wolTimeout) { String macAddress; byte[] hwAddress; WakeOnLanManager wakeOnLanManager = new WakeOnLanManager(); IPAddress ipAddress = null; using (Settings xmlreader = new MPSettings()) { macAddress = xmlreader.GetValueAsString("macAddress", hostName, null); } if (wakeOnLanManager.Ping(hostName, 100) && !string.IsNullOrEmpty(macAddress)) { Log.Debug("WakeUpServer: The {0} server already started and mac address is learnt!", hostName); return true; } // Check if we already have a valid IP address stored, // otherwise try to resolve the IP address if (!IPAddress.TryParse(hostName, out ipAddress) && string.IsNullOrEmpty(macAddress)) { // Get IP address of the server try { IPAddress[] ips; ips = Dns.GetHostAddresses(hostName); Log.Debug("WakeUpServer: WOL - GetHostAddresses({0}) returns:", hostName); foreach (IPAddress ip in ips) { Log.Debug(" {0}", ip); ipAddress = ip; // Check for valid IP address if (ipAddress != null) { // Update the MAC address if possible hwAddress = wakeOnLanManager.GetHardwareAddress(ipAddress); if (wakeOnLanManager.IsValidEthernetAddress(hwAddress)) { Log.Debug("WakeUpServer: WOL - Valid auto MAC address: {0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}" , hwAddress[0], hwAddress[1], hwAddress[2], hwAddress[3], hwAddress[4], hwAddress[5]); // Store MAC address macAddress = BitConverter.ToString(hwAddress).Replace("-", ":"); Log.Debug("WakeUpServer: WOL - Store MAC address: {0}", macAddress); using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.MPSettings()) { xmlwriter.SetValue("macAddress", hostName, macAddress); } } else { Log.Debug("WakeUpServer: WOL - Not a valid IPv4 address: {0}", ipAddress); } } } } catch (Exception ex) { Log.Error("WakeUpServer: WOL - Failed GetHostAddress - {0}", ex.Message); } } Log.Debug("WakeUpServer: WOL - Use stored MAC address: {0}", macAddress); try { hwAddress = wakeOnLanManager.GetHwAddrBytes(macAddress); // Finally, start up the server Log.Info("WakeUpServer: WOL - Start the {0} server", hostName); if (WakeupSystem(hwAddress, hostName, wolTimeout)) { Log.Info("WakeUpServer: WOL - The {0} server started successfully!", hostName); return true; } else { Log.Error("WakeUpServer: WOL - Failed to start the {0} server", hostName); } } catch (Exception ex) { Log.Error("WakeUpServer: WOL - Failed to start the server - {0}", ex.Message); } return false; }