c# - SetAccessControl giving error as An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll -
i need set access control folder
my code
private sub cmdapplyrestrictions_click(sender object, e eventargs) handles cmdapplyrestrictions.click dim mydirectoryinfo new directoryinfo(txtfolder.text) dim mydirectorysecurity directorysecurity = mydirectoryinfo.getaccesscontrol() dim user string = system.environment.userdomainname + "\" + cmbuser.selecteditem.tostring() mydirectorysecurity.addaccessrule(new filesystemaccessrule(user, filesystemrights.read, accesscontroltype.deny)) mydirectoryinfo.setaccesscontrol(mydirectorysecurity) messagebox.show("permissions altered successfully") end sub
the line
mydirectoryinfo.setaccesscontrol(mydirectorysecurity)
is giving exception
an unhandled exception of type 'system.unauthorizedaccessexception' occurred in mscorlib.dll
i logged in user administrative rights not administrator need block access users including logged in user user including administrator
later when program ends, restore permissions
one more requirement wish grant access folder 1 external program
you need app executed elevated privileges.
add app.manifest file app content:
<?xml version="1.0" encoding="utf-8"?> <asmv1:assembly manifestversion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <assemblyidentity version="1.0.0.0" name="myapplication.app"/> <trustinfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedprivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <requestedexecutionlevel level="requireadministrator" uiaccess="false" /> </requestedprivileges> </security> </trustinfo> </asmv1:assembly>