C # how to determine whether a folder is shared and the access permission is read-only

Source: Internet
Author: User

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 );

}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.