Esempio n. 1
0
        /// <summary>
        /// Checks the folder.
        /// </summary>
        /// <param name="taskFolder">The task folder.</param>
        private void CheckFolder(TaskFolder taskFolder)
        {
            TaskFolderCollection taskGroupCollection  = taskFolder.SubFolders;
            List <TaskFolder>    taskFolderCollection = taskGroupCollection.ToList();

            IEnumerable <TaskFolder> taskFolders = from c in taskFolderCollection where c.Name == SchedulerGroupName select c;

            if (taskFolders.Any())
            {
                return;
            }
            taskFolder.CreateFolder(SchedulerGroupName, taskFolder.GetAccessControl(AccessControlSections.Access));
            var directory = new DirectoryInfo(Path.Combine("C:\\Windows\\System32\\Tasks", SchedulerGroupName));

            if (!directory.Exists)
            {
                return;
            }
            DirectorySecurity dSecurity = directory.GetAccessControl();
            var allUsers = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);

            dSecurity.AddAccessRule(new FileSystemAccessRule(allUsers, FileSystemRights.ListDirectory, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));

            var admins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

            dSecurity.AddAccessRule(new FileSystemAccessRule(admins, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));

            var owner = new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null);

            dSecurity.AddAccessRule(new FileSystemAccessRule(owner, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));

            // Set the new access settings.
            var ntAccount = new NTAccount(Environment.UserDomainName, Environment.UserName);

            try
            {
                dSecurity.SetOwner(ntAccount);
                directory.SetAccessControl(dSecurity);
            }
            catch (Exception exception)
            {
                Logger.LogMessage(exception.Message, "SetSchedulerPermission", LogType.Error, exception);
            }
        }