Back to Devexpress

EnvironmentPolicy Class

corelibraries-devexpress-dot-data-dot-utils-549369c0.md

latest6.9 KB
Original Source

EnvironmentPolicy Class

Allows you to spot, analyze, and prohibit unwanted requests to System.Environment initiated by DevExpress controls.

Namespace : DevExpress.Data.Utils

Assembly : DevExpress.Data.v25.2.dll

NuGet Package : DevExpress.Data

Declaration

csharp
public static class EnvironmentPolicy
vb
Public Module EnvironmentPolicy

Remarks

DevExpress UI controls can access (read and write) information about the environment in which an application is running. The EnvironmentPolicy allows you to apply global environment access restrictions, or track app initiated requests and execute custom actions in response.

Use the following methods at application startup to apply a restrictive policy:

The following example suppresses all requests to System.Environment initiated by DevExpress controls:

csharp
using System;
using System.Windows.Forms;

namespace EnvironmentPolicyDemo {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            DevExpress.Data.Utils.EnvironmentPolicy.SuppressAll();
            Application.Run(new Form1());
        }
    }
}
vb
Imports System
Imports System.Windows.Forms

Namespace EnvironmentPolicyDemo
    Friend Module Program
        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread>
        Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            DevExpress.Data.Utils.EnvironmentPolicy.SuppressAll()
            Application.Run(New Form1())
        End Sub
    End Module
End Namespace

Use the following methods to trace and handle exceptions:

  • ThrowAlways() – Throws an exception when the DevExpress control accesses System.Environment.
  • ThrowOnErrors() – Throws an exception if a request to System.Environment fails.

Handle the Failed event to respond to associated failures.

csharp
using System;
using System.Windows.Forms;

namespace EnvironmentPolicyDemo {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            DevExpress.Data.Utils.EnvironmentPolicy.ThrowOnErrors();
            DevExpress.Data.Utils.EnvironmentPolicy.Failed += EnvironmentPolicy_Failed;
            Application.Run(new Form1());
        }

        static void EnvironmentPolicy_Failed(object sender, DevExpress.Data.Utils.EnvironmentPolicy.FailedEventArgs e) {
            Console.WriteLine(e.Exception.Message);
            e.Throw = false;
        }
    }
}
vb
Imports System
Imports System.Windows.Forms

Namespace EnvironmentPolicyDemo
    Friend Module Program
        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread>
        Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            DevExpress.Data.Utils.EnvironmentPolicy.ThrowOnErrors()
            AddHandler DevExpress.Data.Utils.EnvironmentPolicy.Failed, AddressOf EnvironmentPolicy_Failed
            Application.Run(New Form1())
        End Sub

        Private Sub EnvironmentPolicy_Failed(ByVal sender As Object, ByVal e As DevExpress.Data.Utils.EnvironmentPolicy.FailedEventArgs)
            Console.WriteLine(e.Exception.Message)
            e.Throw = False
        End Sub
    End Module
End Namespace

Handle the following events to allow or cancel environment-related operations based on a specific condition:

Tip

Read the following topic for additional information: Environment Policy.

Inheritance

Object EnvironmentPolicy

See Also

EnvironmentPolicy Members

DevExpress.Data.Utils Namespace