How to check/switch the airplane mode programmatic

2019-07-05 22:21发布

问题:

I have to check if the Airplane Mode is enabled in Windows 8 and maybe switch its state. I am currently working on a C# .NET 4.0 Windows Forms application but the answers in this question shouldn't be limited by that.

回答1:

Unfortunately, there isn't a programmatic way for Metro apps to change the airplane mode in Windows 8. It is against the Metro guidelines for an application to go outside its sandbox and modify system settings like this without user permission (see the discussion at http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1ad10725-b1b8-4723-b2c3-861900809e02).

Now, you may be able to figure out the status by using some of the functionality in the Windows.Networking.NetworkOperators namespace. Specifically, check out the MobileBroadbandRadioState and NetworkDeviceStatus enumerations.

Or, you could prompt the user to make the change by explaining how to access the setting using Windows Key + I, Change PC Settings, Wireless, Airplane Mode.



回答2:

Here is a code snippet to get the NetworkConnectivityLevel which will likely give you what you need to know. I don't know if there is a way to change it. I would doubt it because you would need to also provide a way to pick a network to connect to.

    public static NetworkConnectivityLevel GetNetworkConnectivityLevel()
    {
        ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();

        var ncl = NetworkConnectivityLevel.None;

        if (profile != null)
        {
            ncl = profile.GetNetworkConnectivityLevel();
        }

        return ncl;
    }