Registry Access Policy
- 4 minutes to read
DevExpress UI controls can save, read, and modify configuration settings and options in the system registry. These requests can be initiated in your code or through the internal control engine. The RegistryAccessPolicy allows you to apply global registry access restrictions, or track user/app initiated requests and execute custom actions in response.
Apply a Policy
Use the following methods to apply a restrictive policy (call these methods at application startup):
- SuppressAllOperations() – Suppresses all requests to the system registry initiated by DevExpress controls.
- SuppressReadOperations() – Suppresses read requests from the system registry.
- SuppressWriteOperations() – Suppresses requests to write to the system registry.
- ThrowAlways() – Applies a policy to suppress all requests to the system registry initiated by DevExpress controls. Throws an exception when the control accesses the system registry.
- ThrowOnErrors() – Throws an exception if a request to the system registry fails.
The following example suppresses all read/write requests to the system registry:
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DevExpress.Data.Utils.RegistryAccessPolicy.SuppressAllOperations();
Application.Run(new Form1());
}
Allow Specific Registry Operations
Use the RegistryAccessPolicy.AllowOperation method to allow a specific registry operation when a restrictive policy is activated.
Registry operations include:
RegistryOperation.QueryValue
RegistryOperation.SetValue
RegistryOperation.DeleteSubKeyTree
RegistryOperation.EnumerateSubKeyTree
RegistryOperation.All
RegistryOperation.None
The following example suppresses all registry-related operations except for read requests from the system registry:
using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;
using DevExpress.Data.Utils.Registry.Internal;
namespace DXApplication {
internal static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
RegistryAccessPolicy.SuppressAllOperations();
RegistryAccessPolicy.AllowOperation(RegistryOperation.EnumerateSubKeyTree);
RegistryAccessPolicy.AllowOperation(RegistryOperation.QueryValue);
Application.Run(new Form1());
}
}
}
Tip
Use the RegistryAccessPolicy.IsOperationAllowed method to check whether the specified registry operation is allowed.
Manage Registry Operations using Events
Handle the following events to allow or cancel registry operations based on a specific condition:
Policy Event | Description |
---|---|
SetValue | Fires when the DevExpress control attempts to write a value to a system registry key and allows you to cancel the operation. |
QueryValue | Fires when the DevExpress control attempts to read a value from a system registry key and allows you to cancel the operation. |
DeleteValue | Fires when the DevExpress control attempts to delete a value from a system registry key and allows you to cancel the operation. |
EnumerateSubKeyTree | Fires when the DevExpress control attempts to iterate subkey values in the system registry and allows you to cancel the operation. |
CreateSubKey | Fires when the DevExpress control attempts to create a subkey in the system registry and allows you to cancel the operation. |
DeleteSubKeyTree | Fires when the DevExpress control attempts to delete a subkey from the system registry and allows you to cancel the operation. |
The following example suppresses attempts to write values to the specified system registry path:
static string privatePath = "SPECIFY_REGISTRY_PRIVATE_PATH";
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DevExpress.Data.Utils.RegistryAccessPolicy.SetValue += RegistryAccessPolicy_SetValue;
Application.Run(new Form1());
}
static void RegistryAccessPolicy_QueryValue(object sender, DevExpress.Data.Utils.RegistryAccessPolicy.ValueEventArgs e) {
e.Cancel = e.Key.Contains(privatePath);
}
Handle Exceptions
DevExpress UI controls catch registry-related errors or exceptions (error swallowing), and then continue without logging, processing, or reporting errors. Call the RegistryAccessPolicy.ThrowOnErrors method at application startup to manually handle registry-related errors. The ThrowOnErrors
method applies a policy that allows all registry-related operations. The policy throws an exception when a registry operation fails in a DevExpress control.
You can also use the RegistryAccessPolicy.ThrowAlways method to apply a policy that disables all registry-related operations (read, write, enumerate, delete) in DevExpress controls. The policy throws an exception when a DevExpress control attempts to initiate a registry operation.
Handle the RegistryAccessPolicy.Failed event to respond to associated failures.
using DevExpress.Data.Utils;
using DevExpress.Data.Utils.Registry.Internal;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
RegistryAccessPolicy.ThrowAlways();
RegistryAccessPolicy.Failed += RegistryAccessPolicy_Failed;
Application.Run(new Form1());
}
static void RegistryAccessPolicy_Failed(object sender, RegistryAccessPolicy.FailedEventArgs e) {
if(e.Operation == RegistryOperation.QueryValue || e.Operation == RegistryOperation.EnumerateSubKeyTree) {
e.Throw = false;
}
}
Tip
Use the RegistryAccessPolicy.AllowOperation method to allow a specific registry operation when a restrictive policy is activated.