Use C # To add the write permission of NetworkService to the folder

Source: Internet
Author: User
General BS Program This requires that the network service user has the write permission on the folder. In general, we use the script of the package to call the package to set permissions, manual setting is more important when you do not package. It is not complicated to set and deploy only one or two folders. However, when the system can install modules, the number of modules may be large, but it is difficult to put all the files in a directory. In this case, it is advantageous to allow the program to set specific permissions dynamically.
It took half a day to check the information. The first thought was to see if the system had such a command. Right away, CMD typed help and press Enter. A cacls seemed to have seen hope, after a brief look at the help information, search for other people's experience, and finally find that this is only a modification to the file operation permissions, and cannot set permissions on folders. Finally in the http://www.codeproject.com/dotnet/NTSecurityNET.asp found a class library written with VC ++, very convenient to use, also relatively simple, effective after testing. Close up to share with you. Here is an example I wrote: String Accountname = System. environment. Username;
String System = System. environment. machinename;
Accountname =   " NETWORK SERVICE " ;
Microsoft. win32.security. windowsuser Wu =   New Microsoft. win32.security. windowsuser (accountname, system );

Securedobject so =   New Securedobject ();
So. objectname =   @" D: \ test " ;
So. objecttype = Securedobjecttype. fileobject;
So. permissions. setaccess (Wu, accessrights. filewriteunsync, aceinheritanceflags. containerinherit | Aceinheritanceflags. objectinherit );

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.