- 1 year ago
How to check if the Windows 10 device is connected to a public or private WiFi?
You can use the NetworkListManager
COM object in Windows to determine whether a Windows 10 device is connected to a public or private WiFi network. Here's an example using Python and the pywin32
library:
pythonimport win32com.client
def get_network_category():
nlm = win32com.client.Dispatch("NetworkListManager")
networks = nlm.GetNetworks(0) # 0 means all networks
for network in networks:
if network.IsConnected:
if network.Category == 0: # 0 represents public network
return "Public"
elif network.Category == 1: # 1 represents private network
return "Private"
elif network.Category == 2: # 2 represents domain network
return "Domain"
return "Unknown"
# Usage
network_category = get_network_category()
print("Network Category:", network_category)
In this example, the get_network_category()
function uses the NetworkListManager
COM object to retrieve information about connected networks. It loops through the available networks and checks if each network is connected. If a connected network is found, it checks the Category
property to determine if it's a public, private, or domain network. The function returns the corresponding category as a string.
Note that you need to have the pywin32
library installed to run this code. You can install it using pip install pywin32
.
Keep in mind that the NetworkListManager
approach may not work in all scenarios, such as when the network is not classified correctly or if the API behavior changes in future Windows versions. It's recommended to test and validate the behavior on your target Windows 10 devices.