[.NET][C#] Manager to access Network Information


SUBMITTED BY: Guest

DATE: Dec. 7, 2013, 4:41 p.m.

FORMAT: C#

SIZE: 6.8 kB

HITS: 10265

  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Net.Sockets;
  6. using System.Globalization;
  7. namespace Framework
  8. {
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /// <summary> Manager network. </summary>
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. public class ManagerNetwork
  13. {
  14. #region Gestion du singleton
  15. /// <summary> The instance. </summary>
  16. private static ManagerNetwork instance = null;
  17. /// <summary> The padlock. </summary>
  18. private static readonly object padlock = new object();
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. /// <summary>
  21. /// Prevents a default instance of the <see cref="ManagerNetwork"/> class from being created.
  22. /// </summary>
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. ManagerNetwork()
  25. {
  26. //if (_Log.IsDebugEnabled)
  27. // _Log.DebugFormat("Nouvelle instance de {0}", Assembly.GetExecutingAssembly());
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. /// <summary> Gets the instance. </summary>
  31. ///
  32. /// <value> The instance. </value>
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. public static ManagerNetwork Instance
  35. {
  36. get
  37. {
  38. lock (padlock)
  39. {
  40. if (instance == null)
  41. {
  42. instance = new ManagerNetwork();
  43. }
  44. return instance;
  45. }
  46. }
  47. }
  48. #endregion Gestion du singleton
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /// <summary> Gets the MAC address. </summary>
  51. ///
  52. /// <returns> The MAC address. </returns>
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////
  54. public String GetMacAddress()
  55. {
  56. var macAddr =
  57. (
  58. from nic in NetworkInterface.GetAllNetworkInterfaces()
  59. where nic.OperationalStatus == OperationalStatus.Up
  60. select nic.GetPhysicalAddress().ToString()
  61. ).FirstOrDefault();
  62. return macAddr.ToString(CultureInfo.CurrentCulture);
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////
  65. /// <summary> Gets the Ip6 address. </summary>
  66. ///
  67. /// <returns> The IP 6 address. </returns>
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////
  69. public String GetIP6Address()
  70. {
  71. string iP6Address = String.Empty;
  72. try
  73. {
  74. foreach (IPAddress ipa in Dns.GetHostAddresses(Dns.GetHostName()))
  75. {
  76. if (ipa.AddressFamily == AddressFamily.InterNetworkV6)
  77. {
  78. iP6Address = ipa.ToString();
  79. break;
  80. }
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. throw;
  86. }
  87. return iP6Address;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////
  90. /// <summary> Gets the Ip4 address. </summary>
  91. ///
  92. /// <returns> The IP 4 address. </returns>
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////
  94. public String GetIP4Address()
  95. {
  96. string iP4Address = String.Empty;
  97. try
  98. {
  99. foreach (IPAddress ipa in Dns.GetHostAddresses(Dns.GetHostName()))
  100. {
  101. if (ipa.AddressFamily == AddressFamily.InterNetwork)
  102. {
  103. iP4Address = ipa.ToString();
  104. break;
  105. }
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. throw;
  111. }
  112. return iP4Address;
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////
  115. /// <summary> Gets host name of the local computer. </summary>
  116. ///
  117. /// <returns> The local host name. </returns>
  118. ////////////////////////////////////////////////////////////////////////////////////////////////////
  119. public String GetLocalHostName()
  120. {
  121. return System.Net.Dns.GetHostName();
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////////////////////
  124. /// <summary> Gets a value indicating whether this instance is connected to internet. </summary>
  125. ///
  126. /// <value> true if this object is connected to internet, false if not. </value>
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////
  128. public Boolean IsConnectedToInternet
  129. {
  130. get
  131. {
  132. try
  133. {
  134. HttpWebRequest hwebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
  135. hwebRequest.Timeout = 10000;
  136. HttpWebResponse hWebResponse = (HttpWebResponse)hwebRequest.GetResponse();
  137. if (hWebResponse.StatusCode == HttpStatusCode.OK)
  138. {
  139. return true;
  140. }
  141. else
  142. {
  143. return false;
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. throw;
  149. }
  150. }
  151. }
  152. }
  153. }

comments powered by Disqus