generalinformation-405081-security-registry-access-policy.md
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.
Use the following methods to apply a restrictive policy (call these methods at application startup):
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());
}
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
DevExpress.Data.Utils.RegistryAccessPolicy.SuppressAllOperations()
Application.Run(New Form1())
End Sub
Use the RegistryAccessPolicy.AllowOperation method to allow a specific registry operation when a restrictive policy is activated.
Registry operations include:
RegistryOperation.QueryValueRegistryOperation.SetValueRegistryOperation.DeleteSubKeyTreeRegistryOperation.EnumerateSubKeyTreeRegistryOperation.AllRegistryOperation.NoneThe 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());
}
}
}
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.
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);
}
Private Shared privatePath As String = "SPECIFY_REGISTRY_PRIVATE_PATH"
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
DevExpress.Data.Utils.RegistryAccessPolicy.SetValue += RegistryAccessPolicy_SetValue
Application.Run(New Form1())
End Sub
Shared Sub RegistryAccessPolicy_QueryValue(ByVal sender As Object, ByVal e As DevExpress.Data.Utils.RegistryAccessPolicy.ValueEventArgs)
e.Cancel = e.Key.Contains(privatePath)
End Sub
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;
}
}
Imports DevExpress.Data.Utils
Imports DevExpress.Data.Utils.Registry.Internal
<STAThread>
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
RegistryAccessPolicy.ThrowAlways()
AddHandler RegistryAccessPolicy.Failed, AddressOf RegistryAccessPolicy_Failed
Application.Run(New Form1())
End Sub
Shared Sub RegistryAccessPolicy_Failed(ByVal sender As Object, ByVal e As RegistryAccessPolicy.FailedEventArgs)
If e.Operation = RegistryOperation.QueryValue OrElse e.Operation = RegistryOperation.EnumerateSubKeyTree Then
e.Throw = False
End If
End Sub
Tip
Use the RegistryAccessPolicy.AllowOperation method to allow a specific registry operation when a restrictive policy is activated.