List <filesystemrights> ret = new list <filesystemrights> ();
Directorysecurity dirsec = directory. getaccesscontrol (@ "C: \ test", accesscontrolsections. All );
Authorizationrulecollection rules = dirsec. getaccessrules (true, true, typeof (system. Security. Principal. ntaccount ));
Foreach (filesystemaccessrule rule in Rules)
{
Ret. Add (rule. filesystemrights );
}
The RET contains the access permissions of each account to this directory. You can check the permission and the format is as follows:
Readdata | writedata | appenddata | readextendedattributes | bytes | executefile | deletesubdirectoriesandfiles | readattributes | writeattributes | Delete | readpermissions | changepermissions | takenerowship
**********************************
Check whether the folder has the permission to create a file.
Using system;
Using system. IO;
Using system. Security. accesscontrol;
Namespace filesystemexample
{
Class directoryexample
{
Public static void main ()
{
Try
{
String directoryname = "testdirectory ";
Console. writeline ("adding access control entry for" + directoryname );
// Add the access control entry to the directory.
Adddirectorysecurity (directoryname, @ "mydomain \ myaccount", filesystemrights. readdata, accesscontroltype. Allow );
Console. writeline ("removing access control entry from" + directoryname );
// Remove the access control entry from the directory.
Removedirectorysecurity (directoryname, @ "mydomain \ myaccount", filesystemrights. readdata, accesscontroltype. Allow );
Console. writeline ("done .");
}
Catch (exception E)
{
Console. writeline (E );
}
Console. Readline ();
}
// Adds an ACL entry on the specified directory for the specified account.
Public static void adddirectorysecurity (string filename, string account, filesystemrights rights, accesscontroltype controltype)
{
// Create a new directoryinfo object.
Directoryinfo dinfo = new directoryinfo (filename );
// Get a directorysecurity object that represents
// Current security settings.
Directorysecurity dsecurity = dinfo. getaccesscontrol ();
// Add the filesystemaccessrule to the security settings.
Dsecurity. addaccessrule (New filesystemaccessrule (account,
Rights,
Controltype ));
// Set the new access settings.
Dinfo. setaccesscontrol (dsecurity );
}
// Removes an ACL entry on the specified directory for the specified account.
Public static void removedirectorysecurity (string filename, string account, filesystemrights rights, accesscontroltype controltype)
{
// Create a new directoryinfo object.
Directoryinfo dinfo = new directoryinfo (filename );
// Get a directorysecurity object that represents
// Current security settings.
Directorysecurity dsecurity = dinfo. getaccesscontrol ();
// Add the filesystemaccessrule to the security settings.
Dsecurity. removeaccessrule (New filesystemaccessrule (account,
Rights,
Controltype ));
// Set the new access settings.
Dinfo. setaccesscontrol (dsecurity );
}
}
}