Skip to main content
All docs
V24.2

Environment Policy

  • 4 minutes to read

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.

Apply a Policy

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:

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());
        }
    }
}

Manage Environment Operations using Events

Handle the following events to spot, analyze, allow or cancel environment operations based on a specific condition:

Policy Event Description
ExitingProcess Fires when the DevExpress control attempts to exit the current process.
ExpandingVariables Fires when the DevExpress control attempts to replace names of environment variables using the Environment.ExpandEnvironmentVariables(String) method.
GettingVariable Fires when the DevExpress control attempts to retrieve the value of the environment variable from the current process.
SettingVariable Fires when the DevExpress control attempts to set the value of the environment variable in the current process.
ReadingCurrentProcessData Fires when the DevExpress control attempts to read current process data (for example, CurrentDirectory, ProcessId, ProcessPath).
ReadingSpecialFolder Fires when the DevExpress control attempts to read the path to the system special folder (such as Program Files, Programs, System, or Startup).
SettingCurrentDirectory Fires when the DevExpress control attempts to set the path of the current working directory.

Additional events include:

Policy Event Description
ExpandedVariables Fires after environment variables are expanded within the given string.
GotVariable Fires after the DevExpress control has read the value of the environment variable.
SetVariable Fires after the DevExpress control has set the value of the environment variable.
ReadCurrentProcessData Fires after the DevExpress control has read current process data.
ReadSpecialFolder Fires after the DevExpress control has read the path to the system special folder (such as Program Files, Programs, System, or Startup).
SetCurrentDirectory Fires after the DevExpress control has set the path of the current working directory.

Trace and Handle Exceptions

DevExpress UI controls catch environment-related errors or exceptions (error swallowing), and then continue without logging, processing, or reporting errors. Call the EnvironmentPolicy.ThrowOnErrors method at application startup to manually handle associated failures. The ThrowOnErrors method applies a policy that allows all environment-related operations. The policy throws an exception when a request to System.Environment fails in a DevExpress control.

You can also use the EnvironmentPolicy.ThrowAlways method to apply a policy that disables all environment operations in DevExpress controls. The policy throws an exception when a DevExpress control attempts to initiate an operation.

Handle the Failed event to respond to associated failures.

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;
        }
    }
}