Applying Permissions on any Windows Folder using C#
Applying permissions on folders/directories in Windows is one of the things developers want to control with their applications. Today we are going to look at how can we create such an application in C# which can define any kind of permission to any user account in Windows environment.
Start Visual Studio and create a new C# Windows application. Name it DirectoryPermission and Create an interface which looks similar to the one below:
1. Change the text of your window to Folder Permission or any other you like.
2. Drag two labels, two buttons, 1 textbox and 1 combo box on the form. Name the buttons as SelectDirectorybtn and Permissionbtn and leave the names of others as default.
3. Now we need to add two references, for that right-click your project root and select Add reference.
4. From the Add Reference Menu, add following two highlighted references.
Both of these references actually help us interact with Operating System’s account management and queries.
5. Right click on your Windows Form and Select View Code.
6. First of all we will define the references that we added in Step 4. We’ll also add System.IO and System.Security.AccessControl
1: using System.IO;
2: using System.Security.AccessControl;
3: using System.Management;
4: using System.Management.Instrumentation;
7. Now we are going to define a method which will fill our combobox with names of all user account.
1: publicvoid GetUsers()
3: // This query will query for all user account names in our current Domain
4: SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"