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.v24.2.dll
Declaration
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:
- SuppressAll() – Suppresses all requests to
System.Environment
. No exception is thrown. - SuppressSettingEnvironmentVariables() – Disallows setting environment variables.
- SuppressGettingEnvironmentVariables() – Disallows reading environment variables.
- SuppressReadingCurrentProcessData() – Disallows reading current process data (for example,
CurrentDirectory
,ProcessId
,ProcessPath
). - SuppressExitProcess() – Disallows DevExpress controls to forcibly exit the current process.
- SuppressSettingCurrentDirectory() – Disallows setting the current directory.
- SuppressReadingSpecialFolder(Environment.SpecialFolder) – Disallows getting the path to the specified system special folder.
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());
}
}
}
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.
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;
}
}
}
Handle the following events to allow or cancel environment-related operations based on a specific condition:
- ExitingProcess
- ExpandingVariables
- GettingVariable
- SettingVariable
- ReadingCurrentProcessData
- ReadingSpecialFolder
- SettingCurrentDirectory
Tip
Read the following topic for additional information: Environment Policy.