ConnectivityStatus 类

定义

表示应用程序网络连接的状态。

public ref class ConnectivityStatus abstract sealed
public static class ConnectivityStatus
type ConnectivityStatus = class
Public Class ConnectivityStatus
继承
ConnectivityStatus

示例

下面的示例代码演示如何使用此类根据检查框值更新脱机状态。 在此示例中, CheckedChanged 事件处理程序更新脱机状态。 如果用户将应用程序设置为联机状态,事件处理程序将尝试重新验证用户。 但是,如果身份验证服务器不可用,事件处理程序会将应用程序返回到脱机状态。

private void workOfflineCheckBox_CheckedChanged(
    object sender, EventArgs e)
{
    ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked;
    if (!ConnectivityStatus.IsOffline)
    {
        try
        {
            // Silently re-validate the user.
            ((ClientFormsIdentity)
                System.Threading.Thread.CurrentPrincipal.Identity)
                .RevalidateUser();

            // If any settings have been changed locally, save the new
            // new values to the Web settings service.
            SaveSettings();

            // If any settings have not been changed locally, check 
            // the Web settings service for updates. 
            Properties.Settings.Default.Reload();
        }
        catch (System.Net.WebException)
        {
            MessageBox.Show(
                "Unable to access the authentication service. " +
                Environment.NewLine + "Staying in offline mode.",
                "Warning", MessageBoxButtons.OK, 
                MessageBoxIcon.Warning);
            workOfflineCheckBox.Checked = true;
        }
    }
}
Private Sub workOfflineCheckBox_CheckedChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles workOfflineCheckBox.CheckedChanged

    ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked
    If Not ConnectivityStatus.IsOffline Then

        Try

            ' Silently re-validate the user.
            CType(System.Threading.Thread.CurrentPrincipal.Identity,  _
                ClientFormsIdentity).RevalidateUser()

            ' If any settings have been changed locally, save the new
            ' new values to the Web settings service.
            SaveSettings()

            ' If any settings have not been changed locally, check 
            ' the Web settings service for updates. 
            My.Settings.Reload()

        Catch ex As System.Net.WebException

            MessageBox.Show( _
                "Unable to access the authentication service. " & _
                Environment.NewLine + "Staying in offline mode.", _
                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            workOfflineCheckBox.Checked = True

        End Try

    End If
End Sub

注解

ConnectivityStatus使用 类指定使用客户端应用程序服务的基于 Windows 的应用程序的网络连接。 将 属性设置为 IsOfflinetrue时,客户端服务提供程序使用脱机缓存,而不是尝试访问相应的 Web 服务。 脱机状态基于每个用户和每个应用程序存储。 有关详细信息,请参阅如何:脱机使用客户端应用程序服务

属性

IsOffline

获取或设置一个指示应用程序是否处于脱机模式的值。

适用于

另请参阅