using System; using System.Collections.Generic; using System.Runtime.InteropServices; using Vanara.Extensions; using Vanara.PInvoke.NetListMgr; namespace Vanara.Network { /// Represents a wireless network profile public class NetworkProfile : IDisposable { private INetwork inet; /// Initializes a new instance of the class using the GUID of the network profile. /// The GUID of the network profile. public NetworkProfile(Guid guid) { inet = NetworkListManager.Manager.GetNetwork(guid); } /// Initializes a new instance of the class. /// The INetwork instance. internal NetworkProfile(INetwork inetwork) { inet = inetwork ?? throw new ArgumentNullException(nameof(inetwork)); } /// Returns the category of a network. /// A NLM_NETWORK_CATEGORY enumeration value that specifies the category information for the network. public NLM_NETWORK_CATEGORY Category { get => inet.GetCategory(); set => inet.SetCategory(value); } /// Gets the date and time the network was last connected to. /// The connection time. public DateTime ConnectionTime { get { inet.GetTimeCreatedAndConnected(out var _, out var _, out var low, out var hi); return FileTimeExtensions.MakeFILETIME(Vanara.PInvoke.Macros.MAKELONG64(low, hi)).ToDateTime(); } } /// Returns the connectivity state of the network. /// A NLM_CONNECTIVITY enumeration value that contains a bitmask that specifies the connectivity state of this network. public NLM_CONNECTIVITY Connectivity => inet.GetConnectivity(); /// Gets the date and time the network was created. /// The creation time. public DateTime CreationTime { get { inet.GetTimeCreatedAndConnected(out var low, out var hi, out var _, out var _); return FileTimeExtensions.MakeFILETIME(Vanara.PInvoke.Macros.MAKELONG64(low, hi)).ToDateTime(); } } /// Gets or sets the description for the network profile. /// The description. public string Description { get => inet.GetDescription(); set => inet.SetDescription(value); } /// Gets the type of the network. /// The type of the network. public NLM_DOMAIN_TYPE DomainType => inet.GetDomainType(); /// Gets the GUID of the profile. /// The id. public Guid Id => inet.GetNetworkId(); /// Specifies if the network has any network connectivity. /// If , this network is connected; if , it is not. public bool IsConnected => inet.IsConnected; /// Specifies if the network has internet connectivity. /// If , this network has connectivity to the internet; if , it does not. public bool IsConnectedToInternet => inet.IsConnectedToInternet; /// Gets or sets the name of the network profile. /// The name. public string Name { get => inet.GetName(); set => inet.SetName(value); } /// /// Returns an enumeration of all network connections for a network. A network can have multiple connections to it from different /// interfaces or different links from the same interface. /// /// The instance that enumerates the list of local connections to this network. public NetworkListManager.IEnumerableList NetworkConnections => new NetworkListManager.NetworkConnectionIterator(inet.GetNetworkConnections()); /// Gets all local profiles. /// Array of objects. public static IEnumerable GetAllLocalProfiles() => NetworkListManager.Networks; /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. void IDisposable.Dispose() { if (inet == null) return; Marshal.FinalReleaseComObject(inet); inet = null; } /// Determines whether the specified is equal to this instance. /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. /// The parameter is null. public override bool Equals(object obj) { if (obj is NetworkProfile np) return np.Id == Id; else if (obj is Guid g) return g == Id; return false; } /// Returns a hash code for this instance. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() => Id.GetHashCode(); /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => Name; } }