When C# programs run, they often encounter file or folder permission problems, which leads to failure of the program running. In order to solve this headache, we usually need to read and set file and folder permissions.
Read file and folder permissions
/// <summary> /// Read file and folder permissions /// </summary> /// <param name="path"></param> private void ReadPathRule(string path) { DirectoryInfo di = new DirectoryInfo(path); DirectorySecurity ds = (); foreach (FileSystemAccessRule rule in (true, true, typeof())) { ("----------------------------------"); (); if (( & ) != 0) (()); } }
Set file permissions
/// <summary> /// Add users to the file, full control permissions for everyone user group /// </summary> /// <param name="filePath"></param> static void AddSecurityControll2File(string filePath) { //Get file information FileInfo fileInfo = new FileInfo(filePath); //Get access to this file fileSecurity = (); //Add access permission rules for ereryone user group Full control permissions (new FileSystemAccessRule("Everyone", , )); //Add access permission rules for Users user group Full control permissions (new FileSystemAccessRule("Users", , )); //Set access permissions (fileSecurity); }
Set folder permissions
/// <summary> ///Add users to folder, full control permissions for everyone user group /// </summary> /// <param name="dirPath"></param> static void AddSecurityControll2Folder(string dirPath) { //Get folder information DirectoryInfo dir = new DirectoryInfo(dirPath); //Get all access rights to this folder dirSecurity = (); //Set file ACL inheritance InheritanceFlags inherits = | ; //Add access permission rules for ereryone user group Full control permissions FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", , inherits, , ); //Add access permission rules for Users user group Full control permissions FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", , inherits, , ); bool isModified = false; (, everyoneFileSystemAccessRule, out isModified); (, usersFileSystemAccessRule, out isModified); //Set access permissions (dirSecurity); }
Note: Sometimes the program needs to have administrator permissions when modifying permissions. Please refer to how to run the program as an administrator:
C#/WPF Running the program as an administrator
In Vista and Windows 7 and later operating systems, a UAC (User Account Control) security mechanism has been added. If UAC is turned on, even if the user logs in with administrator privileges, his application will not be able to write to settings such as system directories, system registry, etc. that may affect the normal operation of the system by default. This mechanism greatly enhances the security of the system, but for application developers, we cannot force users to shut down UAC, but sometimes the applications we develop need to run in the form of Administrator.
There are several solutions:
Start by () (recommended)
By adding the application manifest file (too violent and prompted)
Directly modify the properties of the program file (inconvenient to deployment)
Method 1: Start by ()
WPF code:
<Application x:Class="" xmlns="/winfx/2006/xaml/presentation" xmlns:x="/winfx/2006/xaml" xmlns:local="clr-namespace:AdminLoadDemo" Startup="Application_Startup"> <> </> </Application>
WPF code:
private void Application_Startup(object sender, StartupEventArgs e) { /** * When the current user is an administrator, start the application directly * If you are not an administrator, start the program with the launch object to ensure that it is run using the administrator. */ //Get the currently logged in Windows user logo identity = (); principal = new (identity); //Discern whether the current logged in user is an administrator if (()) { //If you are an administrator, run it directly new MainWindow().Show(); } else { //Create a startup object startInfo = new (); = true; = ; = ().; //Set the startup action to ensure that it runs as an administrator = "runas"; try { (startInfo); } catch { return; } //quit (0); }
This is the article about C# realizing the permissions of reading and setting files and folders. For more relevant C# file permissions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!