Posts

Showing posts with the label Encryption and Security

How to programmatically control access to files in C# using FileSecurity objects

Image
A quick demonstration on how to control access to files in real-time. As described in the Microsoft documentation , the control access to a file can be added or removed by obtaining the FileSecurity object from that file, then modified, and then applied back to the file. In this example I choose to change the file access properties of a simple icon file "icon.ico": So that before modifying the properties it can be opened straightforwardly as follows: To demonstrate how we can alter the access properties in real-time, create a C# console application in Visual Studio and add the following code. Note the use of WindowsIdentity.GetCurrent() api used to obtain the necessary username/domain details of the current Windows user: [code language="csharp"] using System; using System.IO; using System.Security.AccessControl; using System.Security.Principal; namespace FileControlAccess { class Program { static void Main(string[] args) { ...